# IDs in the API

Every Ampbase id is a [ULID](https://github.com/ulid/spec): 128 bits, written as 26 characters of Crockford base32. Some kinds of id carry a short prefix ahead of the ULID, so you can tell a config's id from a rollout's at a glance.

| Id | Text form | Example |
|---|---|---|
| Config or policy | `cfg-` + ULID | `cfg-01JBQ0Z5M6V3T8K2R4N7P9W1XA` |
| Rollout | `rol-` + ULID | `rol-01JBQ0Z5M6V3T8K2R4N7P9W1XA` |
| Track | `trk-` + ULID | `trk-01JBQ0Z5M6V3T8K2R4N7P9W1XA` |
| Version of a config | ULID | `01JBQ0Z5M6V3T8K2R4N7P9W1XA` |
| Org | ULID | `01JBQ0Z5M6V3T8K2R4N7P9W1XA` |
| Channel | ULID | `01JBQ0Z5M6V3T8K2R4N7P9W1XA` |

A policy is a config, so its id is a config's. Copy ids exactly as the API or the dashboard gives them to you, prefix included.

You rarely need the org's or the channel's id: a call with an API token may leave `org_id` out and act in the token's org, and a request can name its channel by slug (see [Channels by slug](#channels-by-slug)).

## In JSON

The API speaks Connect, and its JSON carries every id as the text above, in requests and responses alike:

```json
{
  "channel_id": "clickhouse",
  "config_id": "cfg-01JBQ0Z5M6V3T8K2R4N7P9W1XC"
}
```

- An id with the wrong prefix, or none where one is expected, is refused with `invalid_argument` and a message naming the field. A rollout's id sent where a config's is expected fails the request rather than being read as the wrong thing.
- An empty string leaves an optional id unset, the same as leaving the field out.
- Field names may be written in either `snake_case` or `camelCase`. Responses use `snake_case`, the names in the API's proto definitions.

## In binary protobuf

Over binary protobuf, an id field is an `ampbase.ulid.v1.ULID` message, not a string. It holds the 16 bytes of the ULID in two `fixed64` fields: `hi` is the first eight bytes, `lo` the last eight, each read big-endian. There is no prefix on the wire. The field's type says what kind of id it is.

```protobuf
message ULID {
  fixed64 hi = 1;
  fixed64 lo = 2;
}
```

JSON also accepts this form, as an object with `hi` and `lo` as decimal strings, though JSON responses always use the text form.

A few request fields, `org_id` and `channel_id` among them, are plain strings holding the bare ULID text, or for `channel_id` a slug. Their JSON looks the same as a typed id's.

A request field of type `ampbase.api.v1.ChannelRef` names a channel by its id (`id`, a `ULID`) or by its slug (`slug`). In JSON it is one string, read as below.

## Channels by slug

Wherever a request names a channel, `channel_id` takes the channel's slug or its id:

- Text that is a ULID, written as the API writes ids, is the channel's id: 26 characters of uppercase Crockford base32 (digits and letters other than I, L, O and U), the first a digit from 0 to 7. Any other text must be a slug: lowercase letters and digits, joined by single hyphens, at most 63 characters. Anything else, such as uppercase text outside that alphabet, is refused with `invalid_argument`.
- These rules are the same over JSON and binary protobuf. Over binary, a `ChannelRef` sets either its `id`, a `ULID` message, or its `slug`, which must be a slug.
- A channel cannot be created with a slug that is also a ULID: a name that would make one, 26 digits starting with 0 to 7, is refused.
- A slug no channel in your org holds is refused with `not_found`. A token limited to some channels is refused with `permission_denied` instead, the same answer it gets for a channel outside its list, whether or not the slug exists.
- Ids are always accepted, and responses always name channels by id.
- A channel's slug never changes, but once the channel is deleted a new channel may take it. For up to a minute after a channel is created, a request naming its slug may still be refused with `not_found`. Once a channel is deleted, a call naming it, by id or by slug, is refused with `not_found`, as is a call naming an id that was never a channel; for up to a minute after the delete, a call may still be answered for it. For up to a minute after a deleted channel's slug is taken by a new channel, a call naming that slug can be refused with `not_found` rather than reach the new channel; naming the channel by its id avoids that.
