Skip to main content
POST
/
access
/
token
/
exchange
Exchange the Connect Token
curl --request POST \
  --url https://authservice-staging.withbenji.com/access/token/exchange \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: <api-key>' \
  --data '
{
  "exchange_token": "<string>"
}
'
import requests

url = "https://authservice-staging.withbenji.com/access/token/exchange"

payload = { "exchange_token": "<string>" }
headers = {
    "x-api-key": "<api-key>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
const options = {
  method: 'POST',
  headers: {'x-api-key': '<api-key>', 'Content-Type': 'application/json'},
  body: JSON.stringify({exchange_token: '<string>'})
};

fetch('https://authservice-staging.withbenji.com/access/token/exchange', options)
  .then(res => res.json())
  .then(res => console.log(res))
  .catch(err => console.error(err));
<?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://authservice-staging.withbenji.com/access/token/exchange",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode([
    'exchange_token' => '<string>'
  ]),
  CURLOPT_HTTPHEADER => [
    "Content-Type: application/json",
    "x-api-key: <api-key>"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://authservice-staging.withbenji.com/access/token/exchange"

	payload := strings.NewReader("{\n  \"exchange_token\": \"<string>\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("x-api-key", "<api-key>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.post("https://authservice-staging.withbenji.com/access/token/exchange")
  .header("x-api-key", "<api-key>")
  .header("Content-Type", "application/json")
  .body("{\n  \"exchange_token\": \"<string>\"\n}")
  .asString();
require 'uri'
require 'net/http'

url = URI("https://authservice-staging.withbenji.com/access/token/exchange")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"exchange_token\": \"<string>\"\n}"

response = http.request(request)
puts response.read_body
{
    "code": "ok",
    "data": {
        "user_id": 1,
        "merchant_id": 1,
        "token_data": {
            "access_token": "0c371a1c-aa81-11ef-b28c-0a58a9feac02",
            "refresh_token": "05tghegts-aa81-sgte-hgte-u8nhgbvgtio",
            "access_token_expires_at": "Tue, 22 Oct 2024 21:57:02 GMT"
        }
    }
}
{
  "error": "invalid_exchange_token",
  "message": "Invalid exchange token"
}
{
  "error": "expired_exchange_token",
  "message": "The exchange token has expired"
}
The x-api-key header value to be used on this API is your partner API key, which can be generated through the dashboard.
exchange_token
string
required
The string token passed as the first argument to your onSuccess callback after a successful Connect flow (Methods & Events). This is the exchange token produced when the transport layer completes with FLOW_SUCCESS — pass it here, not the raw connect token used to initialize the Benji Connect SDK.

Return values

user_id
integer
required
The Benji Platform ID associated with the user.
merchant_id
integer
required
Your system ID as specified on the Benji Platform.
token_data
object
required
An object representing the access_token, refresh_token to use when using the Benji Platform APIs as well as the access token expiry date. Once an access token expires, you can use the refresh token to obtain a new access token silently without requiring the user to authenticate again.
{
    "code": "ok",
    "data": {
        "user_id": 1,
        "merchant_id": 1,
        "token_data": {
            "access_token": "0c371a1c-aa81-11ef-b28c-0a58a9feac02",
            "refresh_token": "05tghegts-aa81-sgte-hgte-u8nhgbvgtio",
            "access_token_expires_at": "Tue, 22 Oct 2024 21:57:02 GMT"
        }
    }
}
{
  "error": "invalid_exchange_token",
  "message": "Invalid exchange token"
}
{
  "error": "expired_exchange_token",
  "message": "The exchange token has expired"
}
Access tokens are typically valid for 90 days. You can refresh an access token either before it expires proactively, or after calling an API and getting an expired_token 401 status code.
Call the Exchange Token API soon after onSuccess. The exchange token is valid for 15 minutes after the successful callback.
See also Methods & Events in the Benji Connect SDK docs for the shape of onSuccess metadata alongside the token.