
# Ruota il segreto di firma di un endpoint webhook

`POST /v1/webhook-endpoints/{endpoint_id}:rotate-secret` · autorizzazione `webhook-endpoints:write` · tutti i piani

Emette un nuovo segreto di firma e lo restituisce una sola volta (`Cache-Control: no-store`). Durante `previous_secret_ttl_hours` (predefinito 24) ogni consegna porta due firme nell'header `webhook-signature`, una con il segreto vecchio e una con quello nuovo, così puoi passare al tuo ricevitore senza perdere consegne. `0` elimina subito il vecchio segreto (per un segreto compromesso). Una richiesta ripetuta con lo stesso `Idempotency-Key` restituisce l'endpoint senza il segreto e con `secret_redacted: true`.

## Richiesta

| Campo | Tipo | Obbligatorio | Descrizione |
|---|---|---|---|
| `endpoint_id` | path | sì | ID dell'endpoint webhook (`whe_…`). |
| `If-Match` | header | no | Se presente, deve corrispondere all'`ETag` attuale dell'endpoint. |
| `previous_secret_ttl_hours` | integer | no | Da 0 a 168 ore in cui il vecchio segreto continua a firmare; predefinito 24. Il body è facoltativo. |
| `Idempotency-Key` | header | no | Riproduce la risposta (senza il segreto) per una richiesta ripetuta con la stessa chiave. |

```json
{
  "previous_secret_ttl_hours": 24
}
```

## Risposta

`200 OK` con l'endpoint, il nuovo `ETag` e il nuovo segreto.

```json
{
  "id": "whe_01k8a3m5q7t9v1w3x5y7z9b2c4",
  "key": "crm-sync",
  "url": "https://hooks.example.com/keenauth",
  "description": "CRM sync",
  "event_types": ["user.created", "user.updated"],
  "event_schema_versions": {},
  "payload_mode": "thin",
  "status": "enabled",
  "rate_limit_per_second": 50,
  "previous_secret_expires_at": "2026-10-27T10:00:00Z",
  "last_success_at": "2026-10-26T09:12:00Z",
  "failing_since": null,
  "version": 4,
  "created_at": "2026-10-26T09:00:00Z",
  "updated_at": "2026-10-26T10:00:00Z",
  "signing_secret": "idp_whsec_<secret>"
}
```

## Errori

| HTTP | code | Quando |
|---|---|---|
| 400 | `unknown_field` | Il body contiene un campo non elencato sopra. |
| 404 | `not_found` | Nessun endpoint con questo ID in questo ambiente. |
| 412 | `precondition_failed` | `If-Match` non corrisponde alla versione attuale. |
| 422 | `validation_failed` | `/previous_secret_ttl_hours out_of_range`: il valore non è compreso tra 0 e 168. |
| 422 | `idempotency_key_reuse` | `Idempotency-Key` è stato usato con una richiesta diversa. |
| 401 | `unauthenticated` | La credenziale manca, non è valida o è stata revocata. |
| 403 | `insufficient_permissions` | La credenziale non ha `webhook-endpoints:write`. |
| 503 | `service_unavailable` | L'amministrazione dei webhook è temporaneamente non disponibile; riprova dopo il periodo indicato in `Retry-After`. |

## Esempio

```bash
curl -X POST https://api.eu.keenauth.com/v1/webhook-endpoints/whe_01k8a3m5q7t9v1w3x5y7z9b2c4:rotate-secret \
  -H "Authorization: Bearer $KEENAUTH_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 4c6e8a0b-2d4f-4a6c-9e1b-3d5f7a9c1e3b" \
  -d '{"previous_secret_ttl_hours":24}'
```

CLI: `keenauth webhook-endpoints rotate-secret whe_01k8a3m5q7t9v1w3x5y7z9b2c4 --previous-secret-ttl-hours 24`
