# Replace a Connection's credential with a new one of its own method, for the same platform account

`PUT https://api.koil.co/v1/connections/{connectionId}/credential`

Replace a Connection's credential with a new one of its own method, for the same platform account: a new key (`method: "key"`, with `key`: its `config` and `secret`), or a fresh grant (`method: "oauth"`). The credential is first checked with the platform, and refused with `connection_invalid` if the platform rejects it; one for another account is refused with `validation_error` (`subject_mismatch`). Nothing is stored unless both pass. The Connection keeps its id, Profiles and `metadata`, becomes `active` again as a reconnect would (an `invalid` one included), and announces `connection.updated`.

## Authorization

An API key from the Koil admin console, sent as `Authorization: Bearer <key>`. A live key starts `koil_live_` and a sandbox key `koil_sandbox_`; the key alone selects the Organization and mode. See https://docs.koil.co/concepts/authentication.

## Parameters

| Name | In | Required | Type | Description |
| --- | --- | --- | --- | --- |
| `Koil-Version` | header | yes | `"2026-12-01"` | The dated API version the request is written against; it fixes the request and response shapes. Every response echoes the version it is rendered in. |
| `connectionId` | path | yes | string |  |
| `Idempotency-Key` | header | no | string | A unique key for this request, scoped to the Organization and operation. Retrying with the same key and payload returns the first result instead of repeating the work, or runs a request that failed again; the same key with a different payload is refused with `409 conflict`. Keys expire 24 hours after first use, and a request still in flight after 15 minutes releases its key to a retry with the same payload. |

## Request body

`application/json`

Type: OAuthConnectionCredentialReplace | KeyConnectionCredentialReplace

Every named shape is in the [OpenAPI document](https://docs.koil.co/openapi.json) under `components.schemas`.

## Example

Replace a key the brand rotated:

```bash
curl -X PUT "https://api.koil.co/v1/connections/{connectionId}/credential" \
  -H "Authorization: Bearer $KOIL_API_KEY" \
  -H "Koil-Version: 2026-12-01" \
  -H "Content-Type: application/json" \
  -d '{"method":"key","key":{"config":{"appleIssuerId":"57246542-96fe-1a63-e053-0824d011072a","keyId":"8QL3N7Y2ZT"},"secret":{"privateKey":"-----BEGIN PRIVATE KEY-----\nMIGT..."}}}'
```

## Response 200

The Connection, holding its new credential.

- `kind` (`"connection"`, required): The kind of resource this is.
- `mode` (one of `"live"`, `"sandbox"`, required): Which mode the resource belongs to. Live and sandbox resources are separate: a key reads and writes only its own mode's.
- `id` (string, required) (min length 1): Koil Connection identifier.
- `issuerId` (string, required) (min length 1): The issuer the Connection's credential comes from (`GET /v1/issuers`): for `oauth`, its App's, such as `facebook` or `google`; for a key, the one the key is for, such as `app_store_connect`.
- `method` (one of `"oauth"`, `"key"`, required): How the Connection was obtained. `oauth`: an end user authorized your App, through a Connect Session or a grant you created it from. `key`: the brand gave you a key of its own, which you sent to `POST /v1/connections`; there is no App.
- `oauth` (object): What an `oauth` Connection was obtained through. Present exactly when `method` is `"oauth"`.
  - `appId` (string, required) (min length 1): The App the Connection acts through.
- `key` (object): What a `key` Connection holds of its key that Koil returns. Present exactly when `method` is `"key"`.
  - `config` (object, required): The key's public half, as `GET /v1/issuers` declares it (`methods.key.config`), such as the key's id; never its secret.
- `clientUserId` (string) (max length 255): Your own identifier for the end user the Connection belongs to.
- `subject` (string): The platform's id for the account the Connection's credential acts as: for `oauth`, the user who authorized (Google's `sub`, Facebook's user id, Instagram's `user_id`, TikTok's `open_id`); for a key, the account the platform confirmed it belongs to. LinkedIn names none, and a lookup that failed at connect time leaves it absent.
- `status` (one of `"active"`, `"reauthorization_required"`, `"invalid"`, required): `active` while its credential authorizes. `reauthorization_required` while the end user must connect again soon: an `expiring` credential still works until then, while a key the platform refuses (`key_refused`, for which Koil polls nothing and bills nothing) or will not issue a token from (`renewal_failed`) does not. `invalid` once it no longer authorizes, which is final: the end user must connect again, and Koil polls nothing for its Profiles, does not bill them, and deletes the Connection six days after it became `invalid`. Either way, create a Connect Session with this `connectionId` (or, for a key, replace it with `PUT /v1/connections/{connectionId}/credential`), and `connection.updated` announces every change. `statusReason` says why it is not `active`.
- `statusReason` (one of `"renewal_failed"`, `"revoked"`, `"expired"`, `"duplicate"`, `"expiring"`, `"key_refused"`): Why the Connection is not `active`; absent while it is. For `invalid`: `renewal_failed` (the platform refused to renew the credential), `revoked` (the platform refused its token, for example because the end user removed the app), `expired` (the credential ran out with nothing to renew it), `duplicate` (the account it acts as already has another Connection under the same issuer, App where it has one, and `clientUserId`, which keeps its place), or `key_refused` (the platform still refused the key 48 hours after it first did). For `reauthorization_required`: `expiring` (the credential, or the refresh token that renews it, expires within 14 days, by `credential.reauthorizeBy`), or `key_refused` (the platform refused a key whose refusal can be a passing one, such as App Store Connect's 401: Koil asks the platform again for 48 hours, and the Connection is `active` again if it takes the key, or `invalid` if it still refuses it).
- `credential` (object): What the Connection's credential states of itself, wherever the Connection appears: its read, its listing and its events. Absent while Koil holds no credential for the Connection, as once it is `invalid`. The expiry of an access token Koil renews is Koil's concern, so it is not stated here.
  - `scopes` (array of string): The scopes the credential holds, as Koil stores them. Absent when Koil does not know them, which is not a credential of none.
  - `reauthorizeBy` (string, date-time): When the credential stops working unless its end user authorizes again. Where Koil can renew the credential, it is the platform's deadline for renewing it: TikTok's and LinkedIn's refresh tokens expire about a year after authorization. Where nothing renews it (a LinkedIn grant without a refresh token), it is the access token's own expiry. Once a refresh token's deadline has passed, it stays that deadline, or the access token's expiry where that is earlier: it never moves later. Koil makes the Connection `reauthorization_required` 14 days ahead of it; send the end user through a Connect Session with this `connectionId` before then. Absent when nothing but a revocation ends the credential (a key, which issues its own tokens), or while Koil does not know the date: a grant created without `refreshTokenExpiresAt`, until a renewal states it.
- `metadata` (object, required): Your own key-value pairs, up to 50 keys of at most 40 characters with string values of at most 500. An update merges into what is stored, and a key set to `""` is removed.
- `createdAt` (string, date-time, required)
- `updatedAt` (string, date-time, required)

## Errors

Every error has the same shape: `{ "error": { "code", "message", "meta", "requestId" } }`. [Errors →](https://docs.koil.co/concepts/errors)

| Status | Meaning |
| --- | --- |
| 400 | `validation_error`: The request does not match the operation's input schema, fails an admission check, or the platform refused its content. `meta.violations` names every problem. `not_supported`: The platform, collection or resource does not offer this operation, such as exporting a `key` Connection's credential, which Koil never returns. |
| 401 | `unauthorized`: No API key, or a key Koil does not accept (unknown, revoked, or with a prefix that does not match its mode). |
| 403 | `permission_denied`: The API key is valid but lacks a permission the operation requires; `meta.required` names it. Use a key created with that permission. `scope_insufficient`: The credential lacks a scope the operation needs; `meta` names the missing scopes. `connection_invalid`: The platform credential behind the Profile no longer works; the end user must re-authorize. |
| 404 | `not_found`: The resource does not exist in this Organization and mode. |
| 409 | `conflict`: The request collides with current state: an idempotency key reused with a different payload or still in flight, a run already in progress, a publish that has already started, or a resource that already exists (`meta.resourceType` and `meta.resourceId` name it). Retry only when the response carries `Retry-After` (the key's first request is still in flight), after that many seconds. |
| 429 | `rate_limit_exceeded`: Koil's own rate limit for the key, Organization, Profile, or client address is spent (`meta.budgets` names which); retry after `Retry-After` seconds. `platform_rate_limit`: The platform throttled the call or the platform quota is spent; retry after `Retry-After` seconds. |
| 500 | `internal`: Koil or the platform failed, and the status says which: 500 when Koil failed, including a platform refusing how Koil made the call; 502 when the platform failed or answered something Koil cannot use; 504 when Koil's call to the platform timed out. The message is generic; retry with backoff. |
| 502 | `internal`: Koil or the platform failed, and the status says which: 500 when Koil failed, including a platform refusing how Koil made the call; 502 when the platform failed or answered something Koil cannot use; 504 when Koil's call to the platform timed out. The message is generic; retry with backoff. |
| 503 | `service_unavailable`: A service Koil needs to accept the request (API key validation or rate limiting) is unavailable. Nothing was attempted; retry after `Retry-After` seconds. |
| 504 | `internal`: Koil or the platform failed, and the status says which: 500 when Koil failed, including a platform refusing how Koil made the call; 502 when the platform failed or answered something Koil cannot use; 504 when Koil's call to the platform timed out. The message is generic; retry with backoff. |
| default | Any other failure. `error.code` names it; the HTTP status follows from it. |
