This step builds on Step 2: Complete SIWB Authentication. Make sure you have a working Internet Computer identity before proceeding.
What You’ll Accomplish
By the end of this step, you’ll have:- ✅ Implemented the Odin API authentication callback
- ✅ Exchanged your Internet Computer identity for a JWT token
- ✅ Tested the complete authentication flow from Bitcoin wallet to API access
- ✅ Retrieved your user principal and delegation information
Overview: From Identity to API Access
Once you have successfully created your Internet Computer identity through SIWB, the next step is to authenticate with Odin’s API services. This involves:- Signing a timestamp with your identity to prove ownership
- Sending authentication data to Odin’s API endpoint
- Receiving a JWT token for authenticated API access
API Authentication Implementation
Let’s implement the function that handles authentication with Odin’s API:core/auth-callback.ts
Understanding the Authentication Process
TheauthenticateCallback
function performs several key operations:
1
Identity Validation
Verifies that a valid identity was provided and determines its type (DelegationIdentity vs Ed25519KeyIdentity)
2
Timestamp Signing
Creates a current timestamp and signs it with the identity to prove ownership
3
Payload Construction
Builds the authentication payload with the appropriate data format for each identity type
4
API Request
Sends the authentication data to Odin’s API endpoint and handles the response
5
Token Extraction
Extracts and returns the JWT token from the API response
The function handles both DelegationIdentity (from SIWB) and Ed25519KeyIdentity (direct key-based) authentication methods, making it flexible for different use cases.
Environment Configuration
- Development
- Production
Testing the Complete Authentication Flow
Now let’s test the entire authentication flow from Bitcoin wallet to API token:index.ts
Excellent! You’ve successfully completed the full authentication flow and received an API token from Odin.
Understanding the Response
The authentication response contains three important pieces of information:JWT Token
JWT Token
The
token
field contains a JSON Web Token (JWT) that you can use to authenticate API requests to Odin services. This token has an expiration time and should be refreshed when needed.Principal ID
Principal ID
The
principal
field shows your unique Internet Computer principal identifier. This is derived from your Bitcoin wallet and will be consistent across sessions.Identity Object
Identity Object
The
identity
field contains the delegation chain information that proves your authentication. This identity will be used later on to do Odin canister calls.Using Your API Token
Once you have your JWT token, you can use it to make authenticated requests to Odin’s API:Security Considerations
Important Authentication Notes:
- Token Storage: Store JWT tokens securely and never expose them in client-side code
- Token Expiration: Monitor token expiration and implement refresh logic
- HTTPS Only: Always use HTTPS for API requests in production
- Rate Limiting: Respect API rate limits and implement appropriate retry logic
Troubleshooting API Authentication
401 Unauthorized
401 Unauthorized
Token Verification Failed
Token Verification Failed
- Confirm your delegation identity is valid and not expired
- Check that the payload format matches the expected structure
- Verify network connectivity to the Odin API
Invalid Response Format
Invalid Response Format
- Ensure the API response is valid JSON
- Check that you’re sending the correct Content-Type headers
- Verify the API endpoint URL is correct
What’s Next?
Congratulations! You now have a complete working authentication system. If you need help setting up a Bitcoin wallet for testing, proceed to the final step where we’ll show you how to create a sample wallet.Continue to Step 4
Learn how to trade on Odin
Integration Summary
You’ve successfully implemented a complete Odin authentication flow that:- ✅ Prepares authentication with the SIWB canister
- ✅ Signs messages with Bitcoin wallet signatures
- ✅ Creates Internet Computer identities
- ✅ Exchanges identities for API tokens
- ✅ Enables authenticated access to Odin services
The code used in this guide is published at https://github.com/Toniq-Labs/odin-docs/tree/main/demo/getting-started.