# Management API

Use the management API to configure streams and destinations from code or CI and to read your organization’s usage. It’s available on [Pro and Team](./plans-and-billing.md).

Base URL: `https://api.send2bucket.com/v1/manage`. The contract is the OpenAPI 3.1 document at [`/v1/manage/openapi.json`](https://api.send2bucket.com/v1/manage/openapi.json); generate a client from it or read it for every field.

## Authentication

Send an account key as a Bearer token: `Authorization: Bearer s2b_acct_…`. Account keys are made on the dashboard's account page (avatar menu, manage account), act on that organization only, and are shown once. They are not ingest keys: `s2b_ingest_` keys cannot call the management API, and `s2b_acct_` keys cannot send data.

## Endpoints

| Method | Path | Does |
|---|---|---|
| `GET` | `/streams` | Lists the organization's streams with their destination's name, kind and status. |
| `POST` | `/streams` | Creates a stream: `{"name": "web"}`, optionally with `"destination_id"`, `"aggregation_rules"`, `"transform"` and `"flush_seconds"`. Returns `201`. |
| `GET` | `/streams/{stream_id}` | Reads a stream. |
| `PATCH` | `/streams/{stream_id}` | Renames a stream, changes its destination, replaces its aggregation rules, sets its [transform](./transforms.md) or its [flush interval](./delivery-format.md#object-keys): `{"name": "events"}`, `{"destination_id": "…"}`, `{"aggregation_rules": […]}`, `{"transform": "del(.password)"}`, `{"flush_seconds": 15}`. A null `destination_id` detaches it; a null `transform` removes it; a null `flush_seconds` follows the plan. |
| `DELETE` | `/streams/{stream_id}` | Deletes a stream. Its destination stays. Returns `204`, or `409` while aggregation rules send rows to it. |
| `GET` | `/streams/{stream_id}/stats` | Ingestion and delivery counters, and the records the stream's [transform](./transforms.md) dropped and failed on. |
| `GET` | `/destinations` | Lists the organization's destinations, each with the `stream_ids` that deliver to it. Secrets are never returned. |
| `POST` | `/destinations` | Creates a destination: `{"name", "kind", "config", "secret"}`. Returns `201`. |
| `GET` | `/destinations/{destination_id}` | Reads a destination. |
| `PATCH` | `/destinations/{destination_id}` | Changes any of `name`, `kind`, `config` and `secret`. Leave `secret` out to keep the stored one. |
| `DELETE` | `/destinations/{destination_id}` | Deletes a destination no stream delivers to. Returns `204`. |
| `POST` | `/destinations/{destination_id}/verify` | Asks for verification. Returns `202`; read the destination's `status` until it is `active` or `error`. |
| `GET` | `/usage` | Ingested bytes and records per billing period and signal. |

A destination belongs to the organization, and any number of its streams can share it. `config` and `secret` take the fields of the destination's page, for example [S3-compatible storage](./destinations/s3-compatible.md). A new or changed secret makes the destination `unverified` until verification or the first successful delivery succeeds; a stream accepts data either way.

```bash
curl https://api.send2bucket.com/v1/manage/destinations \
  -H "Authorization: Bearer $S2B_ACCOUNT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name":"raw","kind":"s3_compatible","config":{"bucket":"raw","endpoint":"https://s3.example.com"},"secret":{"access_key_id":"…","secret_access_key":"…"}}'

curl https://api.send2bucket.com/v1/manage/streams \
  -H "Authorization: Bearer $S2B_ACCOUNT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name":"web","destination_id":"<the destination id>"}'
```

## Errors

Errors are JSON with an `error` code.

| Status | `error` | Meaning |
|---|---|---|
| `400` | `invalid_json` | The body is not a JSON object. |
| `400` | `invalid_request` | A field is missing or invalid, such as an empty name, an unknown destination kind or another organization's `destination_id`. An invalid aggregation rule adds a `detail`, such as `rule 1: unknown output "median"`, and a transform that does not compile adds the compiler's message. A `flush_seconds` outside 10 to 60 adds `flush_seconds must be an integer from 10 to 60, or null`. |
| `401` | `unauthorized` | Missing, unknown or revoked account key. |
| `403` | `plan_required` | The workspace is not on an active Pro or Team plan. |
| `404` | `not_found` | No such stream or destination in the organization. |
| `409` | `name_taken` | Another stream, or another destination, of the organization has this name. |
| `409` | `destination_in_use` | Streams still deliver to the destination. Point them elsewhere first. |
| `409` | `stream_in_use` | Aggregation rules send their rows to the stream. Change those rules first. |
| `503` | `unavailable` | The management API is not available on this node. |
