Create a recovery code
Request body
UUID of the identity to create a recovery code for.
How long the recovery code remains valid. Accepts duration formats like:
1h(1 hour)30m(30 minutes)24h(24 hours)60s(60 seconds)
selfservice.methods.code.config.lifespan configuration value.The type of flow:
api or browser. Determines the format of the recovery link.Response
The recovery code that can be used to recover the account. Share this code with the user.
A URL to the recovery UI with an empty code field. The user must enter the recovery code at this URL.
Timestamp (RFC3339 format) when the recovery code expires.
Example response
Error responses
- 400: Invalid request body or duration format
- 404: Identity not found
Usage notes
Recovery codes are typically used when:- A user has lost access to their account
- An administrator needs to activate a newly created account
- A user needs to verify their email address
- A passwordless onboarding flow is being used
Security considerations
- Recovery codes are single-use only
- Set appropriate expiration times based on your security requirements
- Transmit codes through secure channels (encrypted email, SMS, etc.)
- Never log or store recovery codes in plaintext
Create a recovery link
Query parameters
URL to redirect the user to after successful recovery. Must be a pre-configured allowed return URL.
Request body
UUID of the identity to create a recovery link for.
How long the recovery link remains valid. Accepts duration formats like:
1h(1 hour)30m(30 minutes)24h(24 hours)60s(60 seconds)
selfservice.methods.code.config.lifespan configuration value.Response
The complete recovery URL. When visited, this link automatically recovers the account and initiates a settings flow.
Timestamp (RFC3339 format) when the recovery link expires.
Example response
Error responses
- 400: Invalid request body or duration format
- 404: Identity not found
Usage notes
Recovery links are commonly used for:- Email-based account recovery flows
- “Magic link” authentication
- Account activation after registration
- Passwordless onboarding
Recovery link vs recovery code
Use recovery links when:- You’re sending recovery via email or another digital channel
- You want a one-click recovery experience
- The user’s device can open URLs directly
- You need to transmit recovery via SMS or phone
- You want the user to manually enter a code
- You need a shorter, more memorizable recovery method
- The recovery UI is already open on the user’s device
Security considerations
- Recovery links are single-use only
- Links include sensitive tokens - transmit via secure channels only
- Set short expiration times for high-security applications
- Validate that
return_toURLs are in your allowlist - Never log recovery links in plaintext
- Consider using recovery codes for SMS/phone delivery
Configuration
Configure recovery link behavior in your Kratos configuration:Best practices
Expiration times
Choose expiration times based on your delivery method:- Email recovery: 1-24 hours
- SMS recovery: 5-15 minutes
- In-app recovery: 5-30 minutes
- Admin-initiated: 24-72 hours
Delivery methods
Email delivery:Rate limiting
Implement rate limiting to prevent abuse:- Limit recovery requests per identity
- Track failed recovery attempts
- Consider CAPTCHAs for self-service recovery
- Monitor for suspicious patterns
Audit logging
Log recovery operations for security monitoring:- When recovery links/codes are created
- Which identity they’re for
- Who created them (admin user/system)
- Whether they were successfully used
- Failed recovery attempts