# Interpoint Agent API Playbook

This contract lets an external agent receive every authorized Interpoint message and submit reactive or proactive responses without bypassing human control.

## Credentials and scopes

An administrator creates an agent integration. Interpoint returns the bearer token and webhook secret once. Store both in a secret manager.

| Scope | Capability |
| --- | --- |
| `conversations:read` | Read permitted conversations and histories |
| `contacts:read` | Read contact names, phone numbers, email, and opt state |
| `events:read` | Receive signed events, inspect delivery logs, and request replay |
| `messages:send` | Send autonomous messages when the control engine permits |
| `drafts:write` | Submit version-bound drafts |
| `escalations:write` | Escalate a conversation to paused human control |
| `control:release` | Return an escalated conversation to the agent |

An empty phone-number allowlist means every number in the workspace. A populated allowlist limits reads, events, and sends to those numbers.

Use the token as `Authorization: Bearer ip_agent_…`. Never put it in a query string or mobile client.

## Required send sequence

1. Read the conversation immediately before sending.
2. Confirm its `control`, `automationPaused`, and `version` values.
3. Submit the message with that exact `expectedConversationVersion`.
4. Supply a stable `Idempotency-Key` header unique to the intended message.
5. Treat HTTP `409` as a mandatory re-read. Never blindly retry it with a newer version.

The same sequence applies to proactive messages. Interpoint has no unversioned agent-send endpoint.

```http
POST /v1/agent/conversations/{conversationId}/messages
Authorization: Bearer ip_agent_…
Idempotency-Key: 39d3e6ea-5d8a-49a4-b765-53adcf842d55
Content-Type: application/json

{
  "body": "I can help with that.",
  "mediaUrls": [],
  "expectedConversationVersion": 42
}
```

Autonomous sends are rejected when the conversation is human controlled, manual only, draft only, automation paused, outside configured business hours, stale, opted out, blocked, outside the credential's number scope, or missing `messages:send`.

## Drafts, escalation, and release

- `POST /v1/agent/conversations/{id}/drafts` accepts `body`, `mediaUrls`, `expectedConversationVersion`, and `idempotencyKey`.
- `POST /v1/agent/conversations/{id}/escalate` accepts `expectedConversationVersion` and `reason`. It atomically changes the conversation to human controlled plus automation paused.
- `POST /v1/agent/conversations/{id}/release` accepts the same fields and returns control to the agent.

Pending drafts are automatically invalidated when the conversation version advances.

## Signed webhook events

If your receiver requires Bearer authentication, enter its token in the
agent's webhook settings. InterPoint sends it as `Authorization: Bearer <token>`
alongside the signature headers. Changing the endpoint clears the saved token
unless you supply a replacement. Use the receiver's token for this field.

Interpoint sends JSON with these headers:

```text
Interpoint-Event-Id: <event UUID>
Interpoint-Timestamp: <Unix seconds>
Interpoint-Signature: v1=<hex HMAC-SHA256>
```

The signed input is the exact UTF-8 string:

```text
{timestamp}.{eventId}.{rawRequestBody}
```

Verify with the webhook secret using a constant-time comparison before parsing the JSON. Reject timestamps more than five minutes old. Deduplicate by event ID, process asynchronously, and return a `2xx` quickly.

```json
{
  "id": "45a6a66b-8b7f-43e5-973d-2c9a8ad47ce1",
  "cursor": 184,
  "type": "message.received",
  "createdAt": "2026-08-31T16:00:00.000Z",
  "data": {
    "message": { "id": "…", "direction": "inbound", "body": "Hello" },
    "conversation": { "id": "…", "version": 42 },
    "contact": { "id": "…", "name": "John Smith", "phoneNumber": "+14045550102" },
    "phoneNumber": { "id": "…", "phoneNumber": "+14045550101" }
  }
}
```

Events include `message.received`, `message.sent`, and `conversation.escalated`. Message events are inserted in the same database transaction as inbound message persistence; delivery is asynchronous.

## Retry, logs, and replay

Non-2xx responses and network failures retry with exponential backoff, capped at one hour. After ten attempts the delivery becomes `dead`.

- `GET /v1/agent/events?after={cursor}` reads the durable event stream.
- `GET /v1/agent/deliveries?after={cursor}` reads attempts, status, response code, and last error.
- `POST /v1/agent/events/replay` with `{ "after": 100, "through": 200 }` requeues eligible deliveries.

Persist the highest fully processed cursor. Cursor order is authoritative for replay, but receivers must still deduplicate by event ID.

## Reference implementation

See [`examples/agent-reference-client.ts`](../../examples/agent-reference-client.ts) for signature verification, immediate conversation re-read, and a guarded send.
# Contact, conversation, and handoff actions

- Search contacts with `GET /v1/agent/contacts?q=...` using `contacts:read`.
- Workspace-wide agents with `contacts:write` can create contacts with `POST /v1/agent/contacts` (name, E.164 phoneNumber, optional email) and edit name or email with `PATCH /v1/agent/contacts/:id`.
- Use `POST /v1/agent/conversations` with a contactId and phoneNumberId to start or reopen a conversation. Use `PUT /v1/agent/conversations/:id/read-state` with `{"read":false}` to mark it unread or `true` to clear the shared unread count. Both require `conversations:write`.
- List, edit, or discard your drafts using GET on the conversation's `/drafts` collection and PATCH or DELETE on `/drafts/:id`. Sending still uses the normal message endpoint with the draft content, current conversation version, sending scope, and idempotency key.
- Get a shareable URL from `GET /v1/agent/conversations/:id/link` with `conversations:read`. Escalation also returns a `threadUrl`. Pass the HTTPS URL to the recipient through your own email or SMS workflow. The page links to the installed mobile app, and reading the thread requires an authorized workspace account.
