> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nodeloc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Grant Node flair

> Let your app put user flair on Node members by its own rules

Each member can wear one piece of [user flair](https://docs.nodeloc.com/en/community/nodes.md#let-an-app-grant-user-flair) next to their name in a Node. A Node owner can hand some flair to your app, and the app then puts it on members or takes it off by its own rules. For example, a check-in app can grant the first level after a 7-day streak and switch to the second level at 30 days.

Both sides have to do their part:

| Who | What to do |
| - | - |
| You (the app author) | Request the `flair` permission in `app.json`, read the flair you can grant in your handler, and return a `flair.set` or `flair.clear` effect |
| The Node owner | Create flair for the levels you describe, select **Granted by an app** under **Who can use it** and enter your app's slug, then run the app in the Node |

If either side is missing, nothing is granted. Granting flair through an app sends no notification and isn't recorded in the Node's mod log.

## 1. Request the permission

Add `flair` to `scopes` in `app.json`. It's an ordinary permission. Once your submitted version is approved, you can use it without extra authorization from staff.

```json theme={null}
{
  "slug": "daily-checkin",
  "name": { "zh_CN": "每日签到", "en": "Daily Check-in" },
  "entry": "src/main.js",
  "kind": "applet",
  "surface": "blocks",
  "placement": "many",
  "scopes": ["kv", "flair"],
  "triggers": [],
  "domains": [],
  "settings": []
}
```

If more than one Node should be able to use the app, set `placement` to `many` so each Node's post gets its own install and data. A `single` app can be placed in only one spot site-wide, so it suits a single Node.

## 2. Describe the levels

Your app can't tell what a flair means. It can only use the owner's order: the first item from `api.flair.list()` is level 1, the second is level 2, and so on. In `readme.md`, tell Node owners how many flair to create, in what order, and how to hand them to your app:

```markdown theme={null}
## Turn on check-in flair in your Node

1. Open **Manage → User flair** in your Node and create 4 flair in this order: 3-day streak, 7-day streak, 30-day streak, 100-day streak.
2. For each one, select **Granted by an app** under **Who can use it** and enter `daily-checkin`.
3. Publish a check-in post in your Node, open **More actions** at the top of it, select **Add app**, and enter `daily-checkin`.
```

If the owner creates fewer flair than you have levels, grant from what's there. With only 2 created, a member on a 30-day streak also gets the second one.

## 3. Grant flair in your handler

| API or effect | Description |
| - | - |
| `api.flair.list()` | Flair the Node owner handed to this app, in the owner's order: `[{ id, text, color, style, icon }]`. `[]` if none |
| `api.flair.current()` | The flair the member wears in the Node, as `{ id, text }`. `null` if none |
| `api.flair.member()` | Whether the member has joined the Node |
| `{ type: "flair.set", flair_id }` | Puts flair on the member. `flair_id` must come from `api.flair.list()` |
| `{ type: "flair.clear" }` | Removes flair this app granted. Does nothing if the member wears other flair |

The three reads show the state when the call starts. They don't include the effects you return in the same call. To show the new flair in the interface you return, use the result you computed.

### Mini app in a post

The example below is a check-in mini app. When a member selects **Check in**, it records the streak and puts on the matching flair once a level is reached.

```javascript theme={null}
// Streak lengths for each flair level. Node owners create flair in the same order.
const LEVELS = [3, 7, 30, 100];

// Today and yesterday in China Standard Time, such as "2026-12-06".
const day = (offsetHours) => new Date(Date.now() + offsetHours * 3600000).toISOString().slice(0, 10);
const today = () => day(8);
const yesterday = () => day(8 - 24);

// Flair for a streak of the given length, or null before level 1 or when no flair was handed to this app.
function earned(flairs, streak) {
  const level = Math.min(LEVELS.filter((days) => streak >= days).length, flairs.length);
  return level > 0 ? flairs[level - 1] : null;
}

export async function render(ctx, api) {
  const record = (await api.kv.get("streak")) ?? { days: 0, last: null };
  return { blocks: view(record, await api.flair.current(), await api.flair.member(), ctx.user) };
}

export async function onAction(ctx, api) {
  if (ctx.action_id !== "checkin") return { blocks: null };
  const record = (await api.kv.get("streak")) ?? { days: 0, last: null };
  const member = await api.flair.member();
  let worn = await api.flair.current();
  if (record.last === today()) return { blocks: view(record, worn, member, ctx.user) };

  const next = { days: record.last === yesterday() ? record.days + 1 : 1, last: today() };
  const effects = [{ type: "kv.set", key: "streak", value: next }];
  const flair = earned(await api.flair.list(), next.days);
  // Grant only to members. Granting to a non-member is rejected, and the check-in record above is dropped with it.
  if (member && flair && flair.id !== worn?.id) {
    effects.push({ type: "flair.set", flair_id: flair.id });
    worn = flair;
  }
  return { blocks: view(next, worn, member, ctx.user), effects };
}

function view(record, worn, member, user) {
  const done = record.last === today();
  return {
    type: "vstack",
    gap: "small",
    children: [
      { type: "text", value: `${record.days}-day streak`, size: "large", weight: "bold" },
      { type: "text", value: worn ? `Current flair: ${worn.text}` : "No flair yet" },
      user && !member ? { type: "text", value: "Join this Node to earn check-in flair" } : null,
      user
        ? { type: "button", label: done ? "Checked in today" : "Check in", action: "checkin", variant: "primary", disabled: done }
        : { type: "text", value: "Log in to check in" }
    ].filter(Boolean)
  };
}
```

In a foreground call, `kv` stores the current member's own data, so each member's streak is separate. The check-in record and the flair go in the same response. If any effect is rejected, none of them apply, so a check-in is never recorded without its flair.

### Node bot

A bot has no interface, so it grants flair on site events. A background call targets the member who triggered the event by default. The example below puts the first flair the owner handed to the app on a member when they post their 10th topic or reply in the Node. In `app.json`, set `kind: "bot"` and `surface: "service"`, subscribe to `topic_created` and `post_created`, and request `kv` and `flair`.

```javascript theme={null}
const GOAL = 10;

export async function onTrigger(ctx, api) {
  if (ctx.event !== "topic_created" && ctx.event !== "post_created") return { effects: [] };

  const key = `posts:${ctx.data.user_id}`;
  const count = ((await api.kv.get(key)) ?? 0) + 1;
  const effects = [{ type: "kv.set", key, value: count }];

  const [flair] = await api.flair.list();
  const worn = await api.flair.current();
  if (flair && count >= GOAL && worn?.id !== flair.id && (await api.flair.member())) {
    effects.push({ type: "flair.set", flair_id: flair.id });
  }
  return { effects };
}
```

In a background call, `kv` is the install's shared area, so the key includes the member's ID. To grant flair to another person involved in the event, such as the post's author in `post_liked`, add `user_id` to the effect. `user_id` must be someone involved in this event.

## 4. Test

A test install isn't in any Node. `api.flair.list()` returns `[]`, and `flair.set` and `flair.clear` are validated but not applied. The response includes a notice with `dry_run: true`. In a test run you can check the check-in record and the interface, but no flair is actually put on.

To test end to end, submit and publish a version. In a Node you manage, create the flair your readme describes and hand them to your app. Then start a topic in that Node and add the app with **Add app** in the **More actions** menu at the top of the topic (see [Embed in a post](https://docs.nodeloc.com/en/miniprogram/publishing.md#embed-in-a-post)), or install it as a Node bot. Use an account that has joined the Node and check that the flair appears next to its name.

## Rules and limits

* **The app's Node** depends on where it's installed. For an app in a post, it's the post's Node. For a Node bot, it's the Node the bot is installed on. A site-wide install has no Node and can't grant flair.
* **The target** can only be the current member. In the foreground it's the person using the app. In the background it's the person who triggered the event by default, and `user_id` can only name someone involved in this event.
* Each call can include at most one `flair.set` or `flair.clear`.
* One app can change a member's flair at most 10 times a day. Setting flair the member already wears succeeds without changing anything and doesn't count.
* The platform doesn't add members to the Node. Check `api.flair.member()` before you declare `flair.set`.
* `flair.clear` removes only flair this app granted. If the member wears flair from a moderator or flair they picked, it does nothing and doesn't return an error.
* After the owner switches a flair back to **Moderators only**, it no longer appears in `api.flair.list()`, and your app can't grant or remove it.

| Error code | Reason |
| - | - |
| `E_FLAIR_DENIED` | The flair isn't in the app's Node, wasn't handed to this app, or `user_id` isn't someone involved in this call |
| `E_FLAIR_NOT_MEMBER` | The target hasn't joined the Node |
| `E_FLAIR_NO_NODE` | The install isn't in any Node |
| `E_FLAIR_QUOTA` | The daily limit for changing this member's flair has been reached |

For the full API list, see [Handlers](https://docs.nodeloc.com/en/miniprogram/handlers.md#node-user-flair).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.