# API channel: connect your own application

> How to create an API inbox, get the inbound and outbound secrets, validate the HMAC-SHA256 signature and where to find the technical docs.

The **API** channel is a generic, webhook-based channel. It lets you connect to TalkVend a channel the platform doesn't handle directly, such as a chat on your website or an in-house system. Your application sends customer messages to TalkVend, and TalkVend delivers your team's replies to your application.

## How it works

The channel has two directions:

- **Inbound**: your application makes a POST to the inbox's webhook URL with the customer's message. TalkVend creates or updates the contact and the conversation.
- **Outbound**: when someone on your team replies, TalkVend makes a POST to the **Callback URL** you provided, with the message to deliver.

Each direction has its own secret.

## Create the inbox

1. In the sidebar, open **Inboxes** and click **New inbox**.
2. Choose **API**.
3. Fill in the inbox **Name**.
4. In **Callback URL**, enter where TalkVend should deliver outbound messages, for example `https://your-app.com/webhook`.
5. Optionally, check **Require a template on the first message**. With it, agents must pick a template to start a conversation instead of free text.
6. Click **Create inbox**.

## Copy the secrets

The last screen, **Inbox created — copy the secrets**, shows:

- the **Webhook URL** your application posts inbound messages to;
- **Inbound secret**: your application sends this value with every inbound message;
- **Outbound secret**: TalkVend uses this value to sign the messages it delivers to your Callback URL.

> **Warning:** for security, the secrets won't be shown again. Copy and keep both before clicking **Copied, close**.

## Message security

Inbound messages: your application sends the inbound secret in the `X-Inbox-Secret` header. Without it, or with a wrong value, the message is rejected.

Outbound messages: TalkVend signs the body of each delivery with HMAC-SHA256, using the outbound secret as the key. The signature goes in the `X-Inbox-Signature` header, in the format `sha256=<signature>`. In your application, compute the same HMAC-SHA256 over the received body and compare it with the header. If it doesn't match, discard the request.

## What each message carries

Inbound, the body is a JSON with two objects:

- `contact`: `phoneNumber` (required, with the country code), and optionally `name` and `sourceId`, the customer's id in your system;
- `message`: `content` (required, the text), and optionally `sourceId`, the message id in your system, and `replyToSourceId`, to quote an earlier message.

Outbound, TalkVend sends a JSON with `event` set to `message.created`, the message data (`message`), the conversation (`conversation`) and the contact (`contact`), plus the customer's `sourceId`.

> **Tip:** always send the message `sourceId`. That way, if your application sends the same message again, it isn't duplicated in the conversation.

## Rotate the secrets

On the inbox page, under **Secrets**, use **Rotate** on the **Inbound secret** or the **Outbound secret**.

- The new value appears only once. Copy it before closing.
- The current secret stops working immediately. Update your application with the new value right away.

To change the **Callback URL**, click **Edit** on the inbox page.

## API channel and API keys

The API channel is not the same as **API keys**. The API channel brings messages from an external channel into an inbox. API keys, under **Settings → API keys**, give external systems access to the account's data through the TalkVend API. Each key has its own permissions and inboxes.

The TalkVend API, used with API keys, is described in the [developer docs](https://talkvend.com/en/developers). To learn about the other inboxes, see [Inboxes](https://talkvend.com/en/help/caixas-de-entrada).
