# End a customer's access

> Set when the access a code started ends, and what YOU(th) sends when it has.

Access that a code started with the term `until_ended` continues until you end it. You end it with
one call, when your customer cancels or stops paying. It needs the scope `access_codes:write`.

## End it

Get a [token](/docs/authentication), then set when the access ends:

```bash
curl --request PUT \
  https://api.youth-platform.io/v1/access-codes/0193b1c2-7a4e-7c1d-9f0a-2b6d5e8f1a34/access \
  --header "Authorization: Bearer $YOUTH_ACCESS_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{ "ends_at": "2027-01-12T00:00:00Z" }'
```

The answer is the code, with the end you set:

```json
{
  "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": "redeemed",
  "redeemed_at": "2026-12-12T09:30:00.375Z",
  "access": { "term": "until_ended", "ends_at": "2027-01-12T00:00:00.000Z" }
}
```

| `ends_at`              | Does                                                                    |
| ---------------------- | ----------------------------------------------------------------------- |
| A time to come         | Access ends then: the end of the paid period, or of your grace period   |
| Left out               | Access ends now                                                         |
| A time that has passed | Access ends now, as if left out                                         |
| `null`                 | Removes an end that has not passed yet: the customer changed their mind |

- **Call when you know.** Send the end as soon as the customer cancels, with the date to come, or on
  the day itself. Both work.
- **Repeat it safely.** The same call twice leaves the same end, and a call that ends an access
  already ended answers `200` with the code as it is. Try again after a timeout without checking
  first. A later call replaces the end while it has not passed.
- **A redemption takes a minute to arrive.** The API hears of a redemption within about a minute.
  A `409` `not_redeemed` for a code your customer has just redeemed is worth repeating a minute
  later.
- **Your rules stay yours.** Billing periods, renewals and grace periods are in your system. YOU(th)
  keeps one date per code: the end you sent.
- **An end that has passed is final.** The code is `ended`: a call that sets a later end, or
  `null`, answers `409`. A customer who returns gets a new code.

## What happens then

When `ends_at` passes, the code answers `ended`. Within about 10 minutes the person can no longer
use what the code gave them in the YOU(th) app, and YOU(th) sends your receiver
[`Access.ended`](/docs/webhooks#accessended) with the reason `ended_by_partner` and your end as its
`occurred_at`; later if YOU(th) can't reach the app then. Stop billing on your own date; the event
confirms it.

`Access.ended` can also come without a call of yours:

| `reason`           | Means                                                  |
| ------------------ | ------------------------------------------------------ |
| `ended_by_partner` | The `ends_at` you set has passed                       |
| `term_over`        | A code with the term `fixed_days` reached its last day |

## When the call is refused

| Answer | `code`                    | Means                                                                                                    |
| ------ | ------------------------- | -------------------------------------------------------------------------------------------------------- |
| `404`  | `not_found`               | No code of yours has this id                                                                             |
| `409`  | `not_redeemed`            | Nobody has redeemed the code: [block it](/docs/access-codes/order#block-a-code-you-will-not-use) instead |
| `409`  | `access_ended`            | Its access has already ended, and the call asks for a later end or for none                              |
| `422`  | `invalid_request`         | `ends_at` is not a time, the body holds a field the call doesn't take, or the code's id isn't an id      |
| `422`  | `ends_at_beyond_term`     | `ends_at` is later than the code's own term: a `fixed_days` code cannot be extended past its days        |
| `503`  | `temporarily_unavailable` | Not now: send the same call again after `Retry-After` seconds                                            |

Every refusal has the body [Errors and rate limits](/docs/errors) describes.
