Hanko Flow API Understanding Guide:About Hanko:Hanko is a modern open source authentication solution and the fastest way you integrate passkeys, 2FA, SSO, and more—with full control over your data. Move between self-hosted and Hanko Cloud anytime. No lock-in. Just Auth how it should be: secure, user friendly, and fully yours.What This Guide Covers: This guide explains the fundamental concepts behind the Hanko Flow API, including how states and actions work, how to initialize and interact with flows, handle validation and technical errors, and configure flow behavior for your applications.Key Technologies:
- Hanko Flow API
- REST endpoints
- JavaScript/TypeScript
- state-driven architecture
- JSON request/response handling
- CSRF protection
- client-server communication
- Basic understanding of REST APIs, HTTP requests and responses
- JSON data format
- Client-side JavaScript development
- Authentication flow concepts
- Understand the Flow API’s state-driven architecture
- Learn about states, actions, and input validation system
- Flow initialization
- Implement proper action execution with input data
- Handle validation errors and user feedback
- Manage technical errors and flow recovery
- Configure flows for different authentication scenarios
- Apply best practices for Flow API integration
Introduction
For most applications, the recommended way to use Hanko is by integrating Hanko Elements. In case you want to build a custom frontend instead of using Hanko Elements, for example when building a native mobile app or if Elements do not match your requirements, the Hanko Flow API and thehanko-frontend-sdk
are the tools you need for that.
The Flow API simplifies frontend development by moving authentication flow logic to the backend. Traditional authentication implementations require frontends to manage complex state transitions, predict next steps, and handle various configuration options. This complexity increases maintenance overhead and makes configuration changes difficult.
The Flow API solves this by maintaining all authentication state on the backend. Your frontend only needs to render the current state and execute available actions, without tracking user inputs or predicting what comes next.
Key takeaways include:
- State-Driven Flow: Each API response represents a state, which determines the available actions and required user inputs.
- Dynamic Input Validation: Inputs come with metadata for client-side validation, while the backend ensures robust validation.
- Error Handling: Detailed error annotations in responses guide the frontend in displaying actionable feedback to users.
Understanding states and actions
Every Flow API response contains all information needed to render the appropriate UI. Each response represents a specificstate
in the authentication flow, and each state defines available actions
that users can perform.
Your frontend implementation requires a handler function for each possible state. When you receive a new response from the backend, the corresponding handler processes it and renders the UI based on the available actions.
Each action includes:
- inputs: Defines what data to collect from the user
- validation rules: Specifies requirements for input values
- execution URL: The endpoint to send the action request to
payload
with additional data specific to that state, such as user information or configuration details needed for UI rendering.
The diagram shows the structure of a flow response:
Initializing flows
The Flow API supports three main flows, each initialized with an emptyPOST
request:
/login
- User authentication flow/registration
- User registration flow/profile
- User profile management flow
/example-flow
endpoint to demonstrate the concepts. Here’s how to initialize and work with any flow:
1
Initialize a new flow
Send an empty
POST
request to the the flow URL, /example-flow
in this example.2
Obtain the response body
When the API successfully created a new flow, the response will look like this:
The response object represents a state called ‘example-state’. It includes a single action, ‘example-action’.
Interacting with flows
After receiving a flow response, you process the current state by calling the appropriate handler function. This handler renders the UI based on the state and available actions.1
Generate the request object
Let’s assume that we’ve obtained an input value ‘example input value’ for the input
field ‘example-input-field’. The request body to be generated would look like this:
Note that the
csrf_token
from the previous state must be included in each request.2
Execute the action
Send the object via a
POST
request to the href
provided by the action. In this case, the href
is
/example-flow?action=example-action@example-flow-id
.3
Obtain the response body
When the request was successful, you receive an object in the
same manner, representing the next state:
Handling validation errors
The Flow API includes detailed validation metadata for each input field, such as minimum and maximum length requirements (min_length
and max_length
). While you can implement client-side validation using this metadata, the backend always performs final validation and returns detailed error information when validation fails:
1
Send invalid input values
Let’s assume that we’ve generated an invalid value for ‘example-input-field’:
2
Execute the action
Send the object via a
POST
request to the href
provided by the action. Again, the href
would be
/example-flow?action=example-action@example-flow-id
.3
Obtain the response body
The API would return an object representing the same state, ‘example-state’, but with error annotations indicating
the validation issue:
As you can see, the
status
field has changed to ‘400’ (Bad Request), indicating that the request was invalid. A
global error object has been added with the code
‘form_data_invalid_error’ and the message
‘Form data invalid’. Additionally, the invalid input field now includes an error object with the code
‘value_too_short_error’, and the message
‘The value is too short’.Handling technical errors
Technical errors (non-4xx HTTP errors) result in an error state that indicates the flow cannot continue. These typically occur due to server issues, network problems, or system failures. Here’s how such responses appear:Configuring flows
Flow behavior is highly configurable to match your application’s requirements:- Hanko Cloud users: Configure flows through the Hanko Console
- Self-hosted users: Use the configuration reference
- Full support: Implement handlers for all possible states and actions to support every available feature
- Selective support: Configure your Hanko project to disable unused features and implement only the handlers you need
Conclusion and outlook
This guide has shown you the core concepts of the Flow API through practical examples:- Initialize flows with empty POST requests to flow endpoints
- Interpret responses by understanding states, actions, and input requirements
- Execute actions by collecting user input and sending proper request payloads
- Handle validation errors with detailed field-level error information
- Manage technical errors by implementing proper error recovery flows