
# アプリケーション認証情報を作成する

`POST /v1/applications/{application_id}/credentials` · 権限 `applications:write` · `Idempotency-Key` オプション

アプリケーションに認証情報を追加します: プラットフォーム生成のクライアント シークレット(`type: client_secret`)または `private_key_jwt` の登録公開キー(`type: public_key`)。

- **クライアント シークレット。** レスポンスは `client_secret` を1回だけ提供します。 再度取得することはできません。 すぐに保存してください。 アプリケーションは最大2つのアクティブなシークレットを保持するため、ダウンタイムなしでシークレットをローテーションできます。
- **公開キー。** `jwk` で公開 JWK を送信します。 プライベート メンバー(`d`、`p`、`q`、`dp`、`dq`、`qi`、`oth`、`k`)を持つか、16 KiB より大きい JWK は拒否されます。 アプリケーションは最大5つのキーを保持します。
- **べき等再生。** 同じ `Idempotency-Key` でリクエストを繰り返すと、シークレットなしで保存されたレスポンスを再生します。本文には `secret_redacted: true` があり、`client_secret` はありません。 シークレットが失われた場合は、新しい認証情報を作成してください。

## リクエスト

| パラメータ | 場所 | 説명 |
|---|---|---|
| `application_id` | yol | Application id (`app_…`). |

Body:

| フィールド | 型 | 説명 |
|---|---|---|
| `type` | string | Required. `client_secret` or `public_key`. |
| `expires_at` | string (date-time) | Optional, `client_secret` only; must be in the future. |
| `jwk` | object | Required for `public_key`; a public JWK, at most 16 KiB. |

## レスポンス

`201 Created` with `Cache-Control: no-store`. The credential object as in the list response; for `client_secret` it also has `client_secret`.

```json
{
  "id": "cred_0000000000000000000000001z",
  "type": "client_secret",
  "secret_last4": "abcd",
  "client_secret": "<shown once>",
  "expires_at": null
}
```

## エラー

| HTTP | `code` | いつ |
|---|---|---|
| 401 | `unauthenticated` | The key is missing, invalid or revoked. |
| 403 | `insufficient_permissions` | The key lacks the required permission. |
| 404 | `not_found` | The application does not exist in this environment. |
| 409 | `credential_limit_reached` | The application already has the maximum number of active secrets (2) or keys (5). |
| 409 | `conflict` | The same public key is already registered for the application. |
| 422 | `validation_failed` | The body is invalid: unknown `type`, missing `jwk`, private JWK members, JWK too large, `expires_at` in the past or sent with `public_key`, or a credential type the application type does not allow (`/type: not_allowed`: `client_secret` only for `web` and `m2m`; `public_key` for `web`, `m2m`, `device` and `management`; `spa` and `native` are public clients and take no credentials). |
| 503 | `service_unavailable` | The credential registry is temporarily unavailable; retry after the `Retry-After` delay. |

## 例

```bash
curl -X POST https://api.eu.keenauth.com/v1/applications/app_6f8d0cjgbmf0gtn2rg6pscp3cg/credentials \
  -H "Authorization: Bearer $KEENAUTH_SECRET_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{"type":"client_secret"}'
```
