loud-car-47842
07/24/2025, 6:27 AMbrief-egg-61061
07/24/2025, 6:27 AMbrief-egg-61061
07/24/2025, 6:28 AMloud-car-47842
07/24/2025, 6:28 AMbrief-egg-61061
07/24/2025, 6:28 AMbrief-egg-61061
07/24/2025, 6:28 AMloud-car-47842
07/24/2025, 7:13 AMSessionTokenExchangeCode
brief-egg-61061
07/24/2025, 7:13 AMbrief-egg-61061
07/24/2025, 7:13 AMSessionTokenExchangeCode
is a feature in Ory Kratos used primarily in native and API-based authentication flows, such as social sign-in for native and mobile apps. When you initiate a login or registration flow with the parameter return_session_token_exchange_code=true
, the response will include a session_token_exchange_code
. This code is a secret that the client can later use to retrieve a session token after the flow has been completed. The process works as follows: 1. Flow Initialization: The client starts a login or registration flow with return_session_token_exchange_code=true
. The response contains the first part of the exchange code. 2. User Authentication: The user completes authentication (e.g., via a social provider). After successful authentication, the user is redirected back to the app with the second part of the code in the code
query parameter. 3. Token Exchange: The native application combines both parts of the code and calls the session token exchange endpoint to obtain a session token, which can then be used for authenticated requests. This mechanism is designed to securely issue session tokens to native applications without relying on cookies, making it suitable for mobile and other non-browser clients. The SessionTokenExchangeCode
is only set if the client has requested it and if the flow is of type api
, and only on creating the flow—not on subsequent updates or retrievals of the flow. This is documented in the API responses for login and registration flows [Ory API Reference](https://raw.githubusercontent.com/ory/docs/master/docs/reference/api.json) and described in the implementation guide for native app authentication flows [Social sign-in for native and mobile apps](https://www.ory.sh/docs/kratos/social-signin/native-apps#implementation).