Authentication
The Underline API uses bearer tokens to authenticate requests. Underline will provide your API tokens during onboarding.
Your API tokens carry privileges based on the authenticated user. If the user is part of a merchant account, the token will carry the privileges of that merchant user. If the user is associated with field services, the token will carry the relevant field service privileges.
All API requests must be made over HTTPS. Calls made over plain HTTP will fail. API requests without authentication will also fail.
Obtaining a Bearer Access Token
Section titled “Obtaining a Bearer Access Token”To interact with the Underline API, you need to obtain a bearer access token using your public and secret API tokens.
Endpoint
Section titled “Endpoint”You can obtain a bearer access token by making a POST request to the following endpoint:
{base_url}/api/auth/tokenRequest
Section titled “Request”The request should include the Content-Type header set to application/x-www-form-urlencoded and the following parameters:
grant_type: Should be set toapi_token.public_token: Your public API token provided by Underline.secret_token: Your secret API token provided by Underline.
Example Using curl
curl -s -H 'Content-Type: application/x-www-form-urlencoded'-d 'grant_type=api_token&public_token=YOUR_PUBLIC_TOKEN&secret_token=YOUR_SECRET_TOKEN'https://api.underline.com/api/auth/tokenReplace YOUR_PUBLIC_TOKEN and YOUR_SECRET_TOKEN with the tokens provided to you during onboarding.
Response
Section titled “Response”You’ll receive a response like this:
{ "token_type": "bearer", "access_token": "eyJh...o", "refresh_token": "eyJr...t", "expires_in": 3600, "user_id": "2a6771f8-4458-442a-9ab1-776b84b328aa"}- token_type: The type of token returned (should be
"bearer"). - access_token: The bearer access token you will use to authenticate API requests.
- refresh_token: The refresh token used to obtain new access tokens when the current one expires.
- expires_in: Time in seconds until the access token expires (e.g.,
3600seconds equals 1 hour). - user_id: Your user ID associated with the token.
Obtaining a New Bearer Access Token Using a Refresh Token
Section titled “Obtaining a New Bearer Access Token Using a Refresh Token”If your access token has expired but you have a valid refresh token, you can obtain a new access token without using your API tokens.
Request
Section titled “Request”grant_type=refresh_tokenrefresh_token=YOUR_REFRESH_TOKEN
Example Using curl
curl -s -H 'Content-Type: application/x-www-form-urlencoded'-d 'grant_type=refresh_token&refresh_token=YOUR_REFRESH_TOKEN'https://api.underline.com/api/auth/tokenReplace YOUR_REFRESH_TOKEN with your actual refresh token.
Response
Section titled “Response”You’ll receive a new access token and possibly a new refresh token:
{ "token_type": "bearer", "access_token": "NEW_ACCESS_TOKEN", "refresh_token": "NEW_REFRESH_TOKEN", "expires_in": 3600, "user_id": "USER_ID"}Notes
- Update Tokens: Replace your old tokens with the new
access_tokenandrefresh_token. - Error Handling: If the refresh token is invalid or expired, you’ll need to obtain new tokens using your API tokens as described earlier.
Refresh Token Duration and Usage
Section titled “Refresh Token Duration and Usage”- Duration: Refresh tokens are valid for 16 hours from the time of issuance.
- Single Use: Refresh tokens are valid for a single use only. Each time you use a refresh token to obtain a new access token, a new refresh token will be issued.
- Token Replacement: Always replace your stored refresh token with the new one provided in the response.
Making Authenticated Requests
Section titled “Making Authenticated Requests”Once you have obtained the bearer access token, you can make authenticated requests to the Underline API by including the Authorization header in your requests:
Authorization: Bearer {access_token}Replace {access_token} with the access_token you received from the authentication response.
Example Using curl
Section titled “Example Using curl”curl -H 'Authorization: Bearer eyJh...o'-H 'Content-Type: application/json'-d '{ "query": "{ user(id: "my-user-id") { id } }" }'https://api.underline.com/api/graphqlNote: Ensure that any quotes inside the JSON string are properly escaped.
Authentication Flow
Section titled “Authentication Flow”Before making API requests to the Underline API, your client application needs to ensure it has a valid access token for authentication. The authentication flow involves checking for existing tokens and obtaining new ones as necessary. This process helps maintain secure and uninterrupted communication with the API.
flowchart TD
Start([Start])
AccessTokenCheck{Is there a valid<br/>access token<br/>that hasn't expired?}
UseAccessToken[Use the existing<br/>access token to<br/>authenticate the API request]
RefreshTokenCheck{Is there a valid<br/>refresh token<br/>that hasn't expired?}
UseRefreshToken[Use the refresh token to<br/>obtain a new access token]
ObtainNewTokens[Obtain new access token<br/>and refresh token<br/>using API tokens]
UseNewAccessToken[Use the new access token to<br/>authenticate the API request]
End([End])
Start --> AccessTokenCheck
AccessTokenCheck -- Yes --> UseAccessToken --> End
AccessTokenCheck -- No --> RefreshTokenCheck
RefreshTokenCheck -- Yes --> UseRefreshToken --> UseNewAccessToken --> End
RefreshTokenCheck -- No --> ObtainNewTokens --> UseNewAccessToken --> End
Implementation Tips
Section titled “Implementation Tips”- Token Storage:
- Use a secure storage mechanism appropriate for your environment (e.g., encrypted files, secure databases, key vaults).
- Automatic Refresh:
- Implement logic to automatically refresh the access token using the refresh token before it expires.
- Schedule the refresh operation slightly before the expiration time to account for network delays.
- Thread Safety:
- If your application is multi-threaded or has concurrent processes, ensure that token refresh operations are thread-safe to prevent race conditions.
- Logging:
- Log authentication events without including sensitive token data.
- Monitor logs for authentication failures to detect potential issues.
Frequently Asked Questions
Section titled “Frequently Asked Questions”What should I do if my bearer token expires?
Section titled “What should I do if my bearer token expires?”If your bearer access token expires, you can use the refresh_token to obtain a new access token without re-authenticating with your API tokens. Follow the steps outlined in Obtaining a New Bearer Access Token Using a Refresh Token.
Can I use the same bearer token for multiple requests?
Section titled “Can I use the same bearer token for multiple requests?”Yes, you can use the same bearer access token until it expires. Be sure to handle token expiration in your application by refreshing the token when necessary.
What if I lose my secret API token?
Section titled “What if I lose my secret API token?”If you lose your secret API token, contact your Underline representative immediately to revoke the compromised token and issue a new one.
Contact Support
Section titled “Contact Support”If you have any questions or need assistance with API authentication, please contact your Underline representative.