# REST API Basics

> Base URL, authentication, error format and the endpoint map.

---

LLMS index: [llms.txt](/llms.txt)

---

Everything the web UI and the CLI can do is available over JSON HTTP. The API
is designed to be called by scripts, terminal users and AI agents.

## Base URL

`pigeon serve` listens on `http://127.0.0.1:8787` by default. Change it with
`--addr` or the `PIGEON_ADDR` environment variable:

```bash
PIGEON_ADDR=127.0.0.1:8788 pigeon serve
```

To call the API from other machines, bind an address they can reach:

```bash
pigeon serve --addr 0.0.0.0:8787
```

Remote requests must then carry a bearer token; see
[Authentication](#authentication) below and
[Security & Deployment](../security/) for the deployment trade-offs.

| Path | Purpose |
| --- | --- |
| `/api/*` | REST API (this guide) |
| `/mcp` | MCP over Streamable HTTP |
| `/` | Embedded web UI (mailbox login and message reader) |

## Conventions

- Request and response bodies are UTF-8 JSON. Send `Content-Type: application/json`.
- Timestamps are Unix seconds (integers), not strings.
- Addresses are objects: `{"name": "Ada", "address": "ada@example.com"}`.
- Resource IDs are integers except token IDs, which are opaque strings.
- List endpoints return arrays, never `null`.

## Authentication

Local requests from a loopback address (`127.0.0.1`, `::1`) are allowed without
credentials — convenient for development and for agents running on the same
machine. Every other request must carry a bearer token:

```bash
curl -H "Authorization: Bearer pigeon_xxx" http://server:8787/api/accounts
```

Create tokens with the CLI or the API:

```bash
pigeon token create --name agent   # prints the token once, to stdout
```

See [Tokens & Auth](../tokens/) for the token endpoints and
[Security & Deployment](../security/) for reverse-proxy caveats.

## Errors

Errors always use the same shape:

```json
{"error": "invalid email address \"nope\""}
```

| Status | Meaning |
| --- | --- |
| `400` | Invalid JSON, missing or invalid parameters |
| `401` | Missing or invalid bearer token (non-loopback request) |
| `404` | Unknown account, mailbox, message or token |
| `405` | Wrong HTTP method for the route |
| `500` | Internal failure (database, encryption) |
| `502` | Upstream IMAP/SMTP failure (connection, login, network) |

`502` responses mean Pigeon could not reach or authenticate with the mail
server; the request may succeed later once connectivity is restored.

## CORS

The server answers `OPTIONS` preflight requests with `204` and allows any
origin, the `Authorization` and `Content-Type` headers, and the methods `GET`,
`POST`, `PUT`, `PATCH`, `DELETE`. Authentication is still enforced on
non-loopback requests.

## Endpoint map

| Area | Endpoints | Guide |
| --- | --- | --- |
| Health & meta | `GET /api/health`, `GET /api/meta` | this page |
| Accounts | `GET/POST /api/accounts`, `PUT/PATCH/DELETE /api/accounts/{id}` | [Accounts](../accounts/) |
| Provider detection | `GET /api/detect?email=` | [Accounts](../accounts/) |
| Mailboxes & sync | `GET /api/accounts/{id}/mailboxes`, `POST /api/accounts/{id}/sync`, `POST /api/mailboxes/{id}/fetch-older` | [Mailboxes & Sync](../mailboxes/) |
| Messages | `GET /api/mailboxes/{id}/messages`, `GET /api/messages/{id}`, `POST /api/messages/{id}/flags`, `GET /api/attachments/{id}` | [Messages & Flags](../messages/) |
| Search | `GET /api/search?q=` (also `from`, `subject`, `since`, `until`, `body`, `account`, `limit`, `offset`) | [Search](../search/) |
| Sending | `POST /api/accounts/{id}/send` | [Sending Mail](../send/) |
| Tokens | `GET/POST /api/tokens`, `DELETE /api/tokens/{id}` | [Tokens & Auth](../tokens/) |
| Settings | `GET/PUT /api/settings` | [Settings](../settings/) |

## Health and meta

`GET /api/health` is public and reports liveness:

```bash
curl http://127.0.0.1:8787/api/health
```

`GET /api/meta` requires authentication and tells a client how it was seen:

```bash
curl http://127.0.0.1:8787/api/meta
```

`loopback` is `false` when the request arrived over the network; a web client
can use it to decide whether to show a token login screen.

---

Backlinks:

- [Documentation](/docs/)
