# Order and read codes

> Order a batch of access codes, read them with their links, and block one.

A batch is one order of codes: how many, what access each grants, and until when they can be
redeemed. Your backend orders a batch with the scope `access_codes:write`, and reads its codes with
`access_codes:read` or `access_codes:write`.

## Order a batch

```bash
curl https://api.youth-platform.io/v1/access-code-batches \
  --header "Authorization: Bearer $YOUTH_ACCESS_TOKEN" \
  --header "Content-Type: application/json" \
  --header "Idempotency-Key: 5f2b6f0e-3a51-4c58-9a57-2a1f6f1d3c11" \
  --data '{
    "count": 1000,
    "label": "december-2026",
    "redeem_by": "2027-12-31T23:59:59Z",
    "access": { "term": "until_ended" }
  }'
```

| Field       | Means                                                                                                      |
| ----------- | ---------------------------------------------------------------------------------------------------------- |
| `count`     | How many codes, at most 20,000 in one order and within your [daily limit](#the-daily-limit)                |
| `label`     | A name of yours for the batch, such as the month or your order number. It is never shown to a customer     |
| `redeem_by` | The last moment a code of this batch can be redeemed, still to come and at most five years ahead. Optional |
| `access`    | What a redeemed code grants: see below                                                                     |

| `access.term` | A redeemed code grants                                                                     |
| ------------- | ------------------------------------------------------------------------------------------ |
| `until_ended` | Access until you [end it](/docs/access-codes/end-access): for a service you bill by period |
| `fixed_days`  | Access for `access.days` days from the redemption, then it ends by itself                  |

The answer is `201` once every code can be read and redeemed, with the batch `ready` and its address
in `Location`:

```json
{
  "id": "0193b1c0-11aa-7e02-8c55-9d3f0a7b6c21",
  "label": "december-2026",
  "status": "ready",
  "count": 1000,
  "redeem_by": "2027-12-31T23:59:59.000Z",
  "access": { "term": "until_ended" },
  "created_at": "2026-12-01T10:00:00.000Z"
}
```

An order must carry an `Idempotency-Key` header of your own, a UUID for example, of at most 255
characters. The same key with the same body answers the first order again, within 24 hours of the
latest attempt, so a request you repeat after a timeout never orders twice.

| Answer | `code`                     | Means                                                                                                  |
| ------ | -------------------------- | ------------------------------------------------------------------------------------------------------ |
| `400`  | `idempotency_key_required` | The order has no `Idempotency-Key`                                                                     |
| `400`  | `idempotency_key_invalid`  | The key is empty, or longer than 255 characters                                                        |
| `409`  | `idempotency_conflict`     | The key was already used with another body: use a new key                                              |
| `409`  | `idempotency_in_progress`  | An order with the key runs now: send it again after `Retry-After` seconds                              |
| `409`  | `app_not_set_up`           | Your organization isn't in the YOU(th) app yet, so no code could be redeemed: ask your YOU(th) contact |
| `422`  | `invalid_request`          | A field is missing or not valid, or the order doesn't take it, such as a misspelt one                  |
| `422`  | `redeem_by_passed`         | `redeem_by` has passed                                                                                 |
| `422`  | `redeem_by_too_far`        | `redeem_by` is more than five years ahead                                                              |
| `422`  | `count_over_limit`         | The order would take you past your [daily limit](#the-daily-limit): nothing was made                   |
| `503`  | `temporarily_unavailable`  | Not now: send the same order again after `Retry-After` seconds                                         |

Making thousands of codes takes a few seconds. When an order can't be finished now, it answers
`503` with `Retry-After`: repeat it, or one that timed out, with the same key and body, and it goes
on with the same batch, never a second one. Every refusal has the body
[Errors and rate limits](/docs/errors) describes.

### The daily limit

Your organization's batches ordered in one day, from 00:00 UTC, hold at most 1,000 codes together,
whatever became of them since. An order that would pass it answers `422` `count_over_limit` and
makes nothing: order fewer, with a new `Idempotency-Key` (the refused key answers another body
`409`), or wait for the next day. A repeat of an order with the same key goes on with its batch and
isn't counted again. Need more? Ask your YOU(th) contact to raise your organization's limit.

## Read a batch

```bash
curl https://api.youth-platform.io/v1/access-code-batches/0193b1c0-11aa-7e02-8c55-9d3f0a7b6c21 \
  --header "Authorization: Bearer $YOUTH_ACCESS_TOKEN"
```

| `status`   | Means                                                                                                  |
| ---------- | ------------------------------------------------------------------------------------------------------ |
| `ready`    | Every code can be read and redeemed                                                                    |
| `creating` | An order answered `503` and hasn't been sent again yet: send it again with the same key                |
| `failed`   | No order went on with it for a day: none of its codes is listed or billed. Order again, with a new key |

## Read the codes

```bash
curl "https://api.youth-platform.io/v1/access-codes?batch_id=0193b1c0-11aa-7e02-8c55-9d3f0a7b6c21" \
  --header "Authorization: Bearer $YOUTH_ACCESS_TOKEN"
```

```json
{
  "items": [
    {
      "id": "0193b1c2-7a4e-7c1d-9f0a-2b6d5e8f1a34",
      "batch_id": "0193b1c0-11aa-7e02-8c55-9d3f0a7b6c21",
      "code": "482957362845",
      "redeem_url": "https://youthai.app.link/?~channel=partners&~feature=access_code&$partner_configuration_system_name=coopvoce&$access_code=482957362845",
      "status": "available",
      "redeemed_at": null,
      "access": { "term": "until_ended", "ends_at": null }
    }
  ],
  "next": "0193b1c2-7a4e-7c1d-9f0a-2b6d5e8f1a34"
}
```

| Field            | Means                                                                                                                                                           |
| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id`             | The code's id: what events and the other routes name. Store it beside the code                                                                                  |
| `code`           | What the customer types. An opaque string: store and show it exactly as it is, without spaces                                                                   |
| `redeem_url`     | The link that redeems it, or `null` when your members redeem in an app of your own: see [How the customer redeems](/docs/access-codes#how-the-customer-redeems) |
| `status`         | Where the code is in [its life](/docs/access-codes#a-codes-life)                                                                                                |
| `redeemed_at`    | When it was redeemed, or `null`                                                                                                                                 |
| `access.ends_at` | When its access ends or ended, or `null` while no end is set                                                                                                    |

- The list holds the codes of `ready` batches alone, in a stable order.
- As JSON, the list comes a page at a time, 100 codes unless `limit` says otherwise (at most
  1,000). Pass a page's `next` as `after` to read the next page; the last page has no `next`.
- `next` is for one pass through the list, not a feed of what is new: a batch that becomes `ready`
  later can sort before codes you have already read. Read a new batch's codes with `batch_id`.
- Send `Accept: text/csv` for one CSV file of every matching code, one code per line. The file is
  not paged: it takes no `after` or `limit`, and holds every code the filters keep, up to 50,000.
  More answer `422` `too_many_codes`: filter by `batch_id` or a time range, or read pages as JSON.
- An `after` that isn't an id, or an `after` or `limit` sent for a CSV file, answers `422`
  `invalid_request`.
- `GET /v1/access-codes/{access_code_id}` reads one code. Another organization's code, or one
  that doesn't exist, answers `404` `not_found`.
- The code and its link are secrets until redeemed: whoever has one can redeem it. Keep them out of
  logs, and hand each to one customer only.
- Use `redeem_url` exactly as it is: do not build it from the code.

### As a CSV file

```bash
curl "https://api.youth-platform.io/v1/access-codes?batch_id=0193b1c0-11aa-7e02-8c55-9d3f0a7b6c21" \
  --header "Authorization: Bearer $YOUTH_ACCESS_TOKEN" \
  --header "Accept: text/csv" \
  --output december-2026.csv
```

The file starts with a header line, and an empty field means `null`:

```text
id,batch_id,code,redeem_url,status,redeemed_at,access_term,access_ends_at
0193b1c2-7a4e-7c1d-9f0a-2b6d5e8f1a34,0193b1c0-11aa-7e02-8c55-9d3f0a7b6c21,482957362845,https://youthai.app.link/?~channel=partners&~feature=access_code&$partner_configuration_system_name=coopvoce&$access_code=482957362845,available,,until_ended,
```

Opening the file in a spreadsheet such as Excel, import the `code` column as text: read as a number,
a code of twelve digits is shown as `4.82957E+11`, not as the code to give your customer.

## Block a code you will not use

A code you handed to nobody, or to the wrong person, can be blocked while nobody has redeemed it,
`available` or `expired`:

```bash
curl --request POST \
  https://api.youth-platform.io/v1/access-codes/0193b1c2-7a4e-7c1d-9f0a-2b6d5e8f1a34/deactivate \
  --header "Authorization: Bearer $YOUTH_ACCESS_TOKEN"
```

It becomes `deactivated` and can never be redeemed; the answer is the code. Blocking it again changes
nothing and answers the same, so repeat it safely after a timeout. It needs the scope
`access_codes:write`.

| Answer | `code`                    | Means                                                                                           |
| ------ | ------------------------- | ----------------------------------------------------------------------------------------------- |
| `404`  | `not_found`               | No code of yours has this id                                                                    |
| `409`  | `already_redeemed`        | Someone redeemed the code: [end its access](/docs/access-codes/end-access) instead              |
| `409`  | `redemption_in_progress`  | Someone is redeeming the code this moment: send the same call again after `Retry-After` seconds |
| `422`  | `invalid_request`         | The code's id in the address isn't an id                                                        |
| `503`  | `temporarily_unavailable` | Not now: send the same call again after `Retry-After` seconds                                   |
