# Threads

> One conversation per threadKey: how Messages are grouped, and what the person sees.

A Thread is one conversation between your agent and the person, and every Message belongs to one. A Message sent without a `threadKey` starts a Thread of its own, a Thread of one. That is what the [Quickstart](https://nibify.app/docs/quickstart) did.

## The threadKey

The `threadKey` is yours: a string of up to 200 characters that names the conversation. Every Message sent with the same key joins the same Thread, in the order Nibify received them, and each one takes the next `sequence` of that Thread. Nibify never parses the key, so use whatever your agent already knows.

`invoice-flow` keys by vendor, so that everything about Kiwi Studio stays together:

TypeScript:

```ts
import { Nibify } from '@nibify/sdk';

const nibify = new Nibify({ apiKey: 'sk_test_…' });

const result = await nibify.ask({
  sender: { name: 'invoice-flow' },
  notification: {
    title: 'Kiwi Studio invoice: $1,240, due Oct 30',
    body: 'Read from the PDF. File it as consulting, or fix it first.',
    quickActions: [
      { name: 'file', label: 'File it' },
      { name: 'fix', label: 'Fix it' },
    ],
  },
  threadKey: 'vendor:kiwi-studio',
});

if (result.status === 'answered') {
  result.response.actionName; // 'file'
}
```

curl:

```bash
curl -X POST https://api.nibify.app/v1/requests \
  -H "Authorization: Bearer sk_test_…" \
  -H "Content-Type: application/json" \
  -d '{
    "sender": { "name": "invoice-flow" },
    "notification": {
      "title": "Kiwi Studio invoice: $1,240, due Oct 30",
      "body": "Read from the PDF. File it as consulting, or fix it first.",
      "quickActions": [
        { "name": "file", "label": "File it" },
        { "name": "fix", "label": "Fix it" }
      ]
    },
    "threadKey": "vendor:kiwi-studio"
  }'
```

Next month's invoice from Kiwi Studio goes to `vendor:kiwi-studio` too, and lands in the same conversation.

## Requests and Notifications together

Both endpoints take a `threadKey`. When the invoice is filed, `invoice-flow` says so in the same Thread:

TypeScript:

```ts
import { Card, Column, Nibify, Text } from '@nibify/sdk';

const nibify = new Nibify({ apiKey: 'sk_test_…' });

const surface = {
  root: 'root',
  dataModel: {},
  components: [
    Card('root', { child: 'body' }),
    Column('body', { children: ['title', 'text'] }),
    Text('title', { text: 'Kiwi Studio invoice filed', variant: 'h4' }),
    Text('text', { text: '$1,420, consulting, due Oct 30.' }),
  ],
};

await nibify.notify({
  sender: { name: 'invoice-flow' },
  notification: {
    title: 'Kiwi Studio invoice filed',
    body: '$1,420, consulting, due Oct 30.',
  },
  threadKey: 'vendor:kiwi-studio',
  surface,
});
```

curl:

```bash
curl -X POST https://api.nibify.app/v1/notifications \
  -H "Authorization: Bearer sk_test_…" \
  -H "Content-Type: application/json" \
  -d '{
    "sender": { "name": "invoice-flow" },
    "notification": {
      "title": "Kiwi Studio invoice filed",
      "body": "$1,420, consulting, due Oct 30."
    },
    "surface": {
      "root": "root",
      "dataModel": {},
      "components": [
        { "id": "root", "component": "Card", "child": "body" },
        {
          "id": "body",
          "component": "Column",
          "children": ["title", "text"]
        },
        {
          "id": "title",
          "component": "Text",
          "text": "Kiwi Studio invoice filed",
          "variant": "h4"
        },
        {
          "id": "text",
          "component": "Text",
          "text": "$1,420, consulting, due Oct 30."
        }
      ]
    },
    "threadKey": "vendor:kiwi-studio"
  }'
```

The Notification joins the Thread and stays a Notification: the key decides where a Message goes, never what it is. One Thread can hold questions, answers and news side by side.

## What the person sees

The Inbox shows a Thread as one line, drawn from its newest Message, with a count of what is waiting in it. Tapping the line opens the whole conversation. A line that stands for several Messages has no buttons: the person opens the Thread to answer.

A Message sent into a Thread that already has Messages opens the Thread when its notification is tapped, rather than the Message alone. Its `quickActions` still work from the lock screen.

When nothing in a Thread is waiting any more, it leaves the Inbox for the History.

## Where a key is unique

A `threadKey` names one Thread within a Project and one of its [Environments](https://nibify.app/docs/environments). The same key sent with an `sk_test_` key and with an `sk_live_` key names two Threads, so a test run never joins a real conversation. Two Projects can use the same key without ever meeting.

## Reading a Thread

`GET /v1/threads/{key}` returns a Thread by your key, with its Messages oldest first. `GET /v1/threads` lists the Threads of the Project, the one that spoke last first. A Thread of one has no key: read its Request with `GET /v1/requests/{id}`. Both are in the [Threads API](https://nibify.app/docs/rest/threads).

## Next

- [Lifecycle and expiry](https://nibify.app/docs/lifecycle-and-expiry): The states of a Request, the receipts of a Notification, and an expiry that happens only when you ask for one.
- [Surfaces](https://nibify.app/docs/surfaces): The UI a Request carries: components, a data model, and the actions that send values back.
- [Threads API](https://nibify.app/docs/rest/threads): Every /v1/threads endpoint: list the Threads of this Project, read one Thread by its key.
