# API channels

An API channel brings messages from your own sources into the inbox: your mobile app, a contact form, another chat tool. Your server posts each customer message with a project API key. The team answers in the inbox, and your server gets each reply as a [webhook](webhooks.md).

## Set up

1. Settings → Channels → API → New channel. Name it after the source, like "Mobile app". Its full address is shown with a copy button.
2. Settings → Integrations → New key. Make a project API key (`opkp_…`) with full access.
3. Settings → Integrations → New webhook. Pick `message.created`. Without one, replies can't reach you: the team sees them as "Not delivered: no webhook", and Settings → Channels warns.

Owners and admins do this.

## Post a message

`POST /api/v1/projects/{projectId}/channels/api/{channelId}/messages`

```bash
curl https://opskeet.com/api/v1/projects/$PROJECT/channels/api/$CHANNEL/messages \
  -H "Authorization: Bearer $OPSKEET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contact": { "externalId": "user_42", "name": "Ada Byron", "email": "ada@example.com" },
    "id": "msg_1001",
    "body": "My export failed twice."
  }'
```

The answer is `201` with `{ "conversationId": "…", "messageId": "…" }`.

| Field                | Needed | What it does                                                                                                     |
| -------------------- | ------ | ---------------------------------------------------------------------------------------------------------------- |
| `contact.externalId` | Yes    | Your ID for the person. The same ID on this channel is the same contact.                                         |
| `contact.name`       | No     | Fills the contact's name if it's empty. The team's edits stay.                                                   |
| `contact.email`      | No     | Fills the contact's email if it's empty. It's never treated as proven.                                           |
| `conversationId`     | No     | Adds the message to this conversation. Without it, a new conversation starts. Keep the ID from the first answer. |
| `id`                 | No     | Your ID for the message. Posting the same `id` again, even at the same time, returns the first result and adds nothing. |
| `body`               | \*     | The text, up to 20,000 characters.                                                                               |
| `attachments`        | \*     | Up to 5 files: `{ "name", "type", "data" }`, with `data` in base64. Each file is up to 10 MB.                    |

\* A message needs a body, a file, or both. A request is up to 20 MB, files as base64 included; a larger one gets `413` with a message saying so.

Only a project API key can post. A person's session or personal key gets `403`, since it would write as the customer. Other answers:

- `404` when the channel isn't there, or `conversationId` isn't this contact's conversation on this channel.
- `403 BLOCKED` when the team blocked the contact.
- `429` past 120 messages an hour for one contact, or 5,000 a day for the project.

A conversation reopens when the customer writes again. The AI agent answers them only when one of your AI assistants takes API messages (Settings → AI); its replies arrive as webhooks like a teammate's. They never show in the chat widget.

## Get the team's replies

Each reply sends `message.created` to your webhooks. The same event also fires for the messages you post, so act only on replies:

```js
// Express, after checking the signature (see webhooks.md).
app.post("/opskeet", (req, res) => {
  const { type, data } = req.body
  const reply =
    type === "message.created" &&
    data.conversation.channel === "api" &&
    data.conversation.channelId === MY_CHANNEL_ID &&
    data.message.kind === "message" &&
    data.message.author.type !== "contact"
  if (reply)
    sendToUser(data.conversation.externalId, {
      text: data.message.body,
      files: data.message.attachments.map((file) => file.url),
      buttons: data.message.buttons,
      conversationId: data.conversation.id,
    })
  res.sendStatus(200)
})
```

- `data.conversation.channelId` is the API channel, and `data.conversation.externalId` is your ID for the person.
- `data.message.author.type` is `teammate`, `ai` or `project` for replies, and `contact` for what you posted.
- Each attachment has its `id` and a signed `url` that works for 7 days. Save the file if you need it longer.
- `data.message.buttons` lists quick replies for the person to pick from. Show them as buttons, or as a numbered list. Post the picked text back as their message; a number they type also works.
- A reply shows "Delivered" in the inbox once one of your webhooks answers 2xx. If every try fails, it shows "Not delivered" with Retry.
- Internal notes come only when the webhook has "Include notes" on, with `message.kind` set to `note`. Skip them.
