Skip to content
Last updated

OAuth integration

Adfin uses OAuth 2.0 for secure authentication and authorisation. Before you can make requests to the Adfin API, you must get an access token.

Prerequisites

Your Adfin Integration Manager will share encrypted app credentials containing your client_id and client_secret.

OAuth 2.0 flows supported

  • Client credentials flow (for platform-to-platform communication)
  • Authorization code flow (for biller authorisation on behalf of your platform)

Authentication in Adfin

You must supply your client_id and client_secret either in the HTTP Basic Auth header or as form parameters. If you include them in both places, they must match exactly.

How to use header authentication

Base64 authentication

The authorization header string is Basic Base64Encode(client_id:client_secret).

For example, for client ID xyz123clientid and client secret secret987654321, first Base64-encode the string:

xyz123clientid:secret987654321
→ eHl6MTIzY2xpZW50aWQ6c2VjcmV0OTg3NjU0MzIx

Then your header becomes:

Authorization: Basic eHl6MTIzY2xpZW50aWQ6c2VjcmV0OTg3NjU0MzIx

Request example

Request
curl -X POST "https://api.staging.adfin.com/api/oauth2/token" \
-H "Authorization: Basic $(echo -n 'YOUR_ID:YOUR_SECRET' | base64)" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'grant_type=client_credentials'

Client credentials flow (platform token)

Use this flow when your platform must access Adfin resources directly.

Request
curl -X POST "https://api.staging.adfin.com/api/oauth2/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'grant_type=client_credentials&client_id={your_client_id}&client_secret={your_client_secret}'

Body:

  • Set grant_type to client_credentials.
  • Replace {your_client_id} and {your_client_secret} with your credentials.

If the request is successful, Adfin responds with an access token:

Response
{
  "access_token": "your_access_token",
  "token_type": "Bearer",
  "expires_in": 3600
}

Authorization code flow

This flow allows your platform to request an access token after receiving an authorization code from a biller's login session.

The biller is prompted to log in to their account.

Adfin login page

Step 1: Redirect the biller to Adfin's authorisation page

Use the hosts for the environment your credentials belong to. The biller login runs on the Adfin Console. Every other call goes to the API.

StepStagingProduction
Biller login and consenthttps://staging.adfin.com/auth/loginhttps://console.adfin.com/auth/login
Token and refreshhttps://api.staging.adfin.com/api/oauth2/tokenhttps://api.adfin.com/api/oauth2/token
Revokehttps://api.staging.adfin.com/api/oauth2/revokehttps://api.adfin.com/api/oauth2/revoke
API callshttps://api.staging.adfin.com/apihttps://api.adfin.com/api

Redirect the biller to Adfin's authorisation page. Construct your unique authorisation URL:

Staging:

https://staging.adfin.com/auth/login?&client_id={your_client_id}&scope=invoices%20customers&redirect_uri={your_redirect_uri}&state={random_state_value}

Production:

https://console.adfin.com/auth/login?&client_id={your_client_id}&scope=invoices%20customers&redirect_uri={your_redirect_uri}&state={random_state_value}
Login address

Use exactly the login addresses above. Adfin's login page builds its return address from the address it is opened on. If you open it on a different address, such as app.adfin.com, the result is a "Callback URL mismatch" error, even when your redirect_uri is correct. If your redirect_uri is not registered, the biller sees "Invalid redirect URI." when they approve access.

Properties required for the OAuth redirect:

URI propertyDescription
client_idThe client_id of your platform, as provisioned by Adfin
redirect_uriThe address Adfin sends the biller to after they grant permission
scopeUse the scopes invoices and customers
stateRequired. A random value you generate and check on the callback. Without it, a biller who is already signed in to Adfin goes to their dashboard instead of the consent screen

Step 2: Handle the callback

After the biller logs in and approves the authorisation, Adfin redirects them to the redirect_uri from Step 1. Adfin appends a code to the query string, for example:

https://your-platform.com/callback?code=[your_authorization_code]&state=random_state_value

Step 3: Exchange the authorization code for tokens

Use this request to exchange the authorization code for an access token and a refresh token.

You can use header authentication or form parameter authentication, as described in Authentication in Adfin.

Request
curl -X POST "https://api.staging.adfin.com/api/oauth2/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'grant_type=authorization_code&client_id={your_client_id}&client_secret={your_client_secret}&code={authorization_code}&redirect_uri={your_redirect_uri}'
Response
{
  "access_token": "your_access_token",
  "refresh_token": "your_refresh_token",
  "expires_in": 3600,
  "token_type": "Bearer"
}
Request dataDescription
grant_typeSet to authorization_code in this step
client_idThe credentials of your Adfin app
client_secretThe credentials of your Adfin app
codeThe authorization code from step 2
redirect_uriMust be the same URI used in the initial request
Response dataDescription
access_tokenThe access token for the biller
refresh_tokenThe refresh token for the biller's account
expires_inHow long the access token is valid for, in seconds
token_typeAlways Bearer

You can now use the access_token to make authorised requests on behalf of the biller, for example to create customers.


Managing refresh tokens

Use the refresh token to get a new access token when the current access token expires. This keeps the session active, so the biller does not have to log in again.

OAuth with refresh token rotation

Our integration uses OAuth 2.0 with refresh token rotation. This means:

  • You must use each newly issued refresh token from then on.
  • Discard old refresh tokens immediately.
  • Concurrent requests must not reuse or fall back to expired tokens.
  • If you send a refresh token that was already used, Adfin can revoke every refresh token on that connection. The biller must then authorise again.

Using refresh tokens

Use refresh tokens to get new access tokens and new refresh tokens.

Request
curl -X POST "https://api.staging.adfin.com/api/oauth2/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'grant_type=refresh_token&client_id={your_client_id}&client_secret={your_client_secret}&refresh_token={your_refresh_token}'
Response
{
  "access_token": "new_access_token",
  "refresh_token": "new_refresh_token",
  "expires_in": 3600,
  "token_type": "Bearer"
}
Request dataDescription
grant_typeSet to refresh_token
client_idYour platform's client_id
client_secretYour platform's client_secret
refresh_tokenThe refresh token you received during the last token exchange
Response dataDescription
access_tokenThe access token for the biller
refresh_tokenThe new refresh token for the biller's account
expires_inHow long the access token is valid for, in seconds
token_typeAlways Bearer

Token expiry

  • access_token: Expires after 1 hour.
  • refresh_token: Expires if it is not used for 5 days. Every refresh returns a new refresh token with a new 5-day window. A connection can stay active for up to 1 year from when the biller authorised it. After 1 year, the biller must authorise again.

Error handling

  • 401 Unauthorized (INVALID_REFRESH_TOKEN): The refresh token has expired, has been revoked or is not valid. Ask the biller to authorise again with the authorization code flow.
  • 400 Bad Request: The request format is incorrect, or a required parameter is missing.

Best practices for using refresh tokens

  • Token rotation: When the response contains a new refresh token, replace the old one in your system.
  • Scheduled refresh: Refresh each connection at least once a day, even when there is no activity for that biller. This makes sure that a refresh token is never unused for 5 days.
  • Token storage: Store refresh tokens securely, for example encrypted, to protect them from theft.
  • Expiry handling: When an access token expires, use the refresh token to get a new one. This prevents login interruptions for the biller.