Authentication
Overview
To securely interact with Paytently's API, all requests must be authenticated using JSON Web Tokens (JWT) using the OAuth client credentials standard flow. This ensures that only authorized users and systems can access the API endpoints. This guide provides a step-by-step explanation of the JWT authentication flow.
At a basic level our authentication flow is as follows:
- Make a POST request our
/auth/tokenendpoint with your credentials - The API response will contain a bearer
access_token - Include this
access_tokenin the header of all API requests:Authorization: Bearer {access_token}
Client credentials
To begin the authentication process, you will have a Client ID and Client Secret provided to you by Paytently.
Your client secrets should be encrypted and stored securely. Once generated, we can regenerate the secret for you, but we cannot retrieve it. Client credentials are linked to an entity and application. To provide flexibility, each application can have multiple sets of credentials if desired.
Request an access token
When requesting a new JWT, you provide your client_id and client_secret.
The grant_type and audience fields are both required and are sent in the form body alongside your credentials. Set grant_type to client_credentials and audience to the URL of the environment you are using.
- Sandbox audience:
https://sandbox.api.paytently.io - Production audience:
https://api.paytently.io
This request uses the x-www-form-urlencoded header content type. This is different to all our other APIs.
Request:
- curl
- PHP
- .NET
- javascript
- Java
curl --location 'https://sandbox.api.paytently.io/auth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'audience=https://sandbox.api.paytently.io' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=your-client-id' \
--data-urlencode 'client_secret=your-client-secret' \
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://sandbox.api.paytently.io/auth/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
],
CURLOPT_POSTFIELDS => http_build_query([
'client_id' => 'YOUR_CLIENT_ID',
'client_secret' => 'YOUR_CLIENT_SECRET',
'grant_type' => 'client_credentials',
'audience' => 'https://sandbox.api.paytently.io'
]),
]);
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Error: ' . curl_error($curl);
} else {
$responseData = json_decode($response, true);
$accessToken = $responseData['access_token'];
}
curl_close($curl);
using (HttpClient client = new HttpClient())
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("client_id", "YOUR_CLIENT_ID"),
new KeyValuePair<string, string>("client_secret", "YOUR_CLIENT_SECRET"),
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("audience", "https://sandbox.api.paytently.io")
});
HttpResponseMessage response = await client.PostAsync("https://sandbox.api.paytently.io/auth/token", content);
string responseString = await response.Content.ReadAsStringAsync();
var responseData = JsonSerializer.Deserialize<ResponseData>(responseString);
var token = responseData.AccessToken;
}
const axios = require('axios');
const qs = require('qs');
const data = qs.stringify({
client_id: 'YOUR_CLIENT_ID',
client_secret: 'YOUR_CLIENT_SECRET',
grant_type: 'client_credentials',
audience: 'https://sandbox.api.paytently.io'
});
axios.post('https://sandbox.api.paytently.io/auth/token', data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
})
.then(response => {
const { access_token, expires_in, scope, token_type } = response.data;
})
.catch(error => {
console.error(error);
});
URL url = new URL("https://sandbox.api.paytently.io/auth/token");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
String data = "client_id=YOUR_CLIENT_ID" +
"&client_secret=YOUR_CLIENT_SECRET" +
"&grant_type=client_credentials" +
"&audience=https://sandbox.api.paytently.io";
try (OutputStream os = connection.getOutputStream()) {
os.write(data.getBytes());
os.flush();
}
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (Scanner scanner = new Scanner(connection.getInputStream())) {
String response = scanner.useDelimiter("\\A").next();
JSONObject responseObject = new JSONObject(response);
String accessToken = responseObject.getString("access_token");
}
} else {
System.out.println("Request failed with response code: " + responseCode);
}
Response:
{
"access_token": "your-jwt-access-token",
"expires_in": 3600,
"scope": "",
"token_type": "bearer"
}
| Response parameter | Description |
|---|---|
access_token | The JWT that will be used in future API requests. |
expires_in | The token's expiration time in seconds. |
scope | The scopes granted to the token, separated by spaces. |
token_type | Always bearer. Send the token in the Authorization: Bearer header. |
Including token in API requests
After receiving the JWT, include it in the Authorization header of each subsequent API request.
You will be able to perform actions:
- Only whilst the token is not expired
- Only for the entity and application assigned to the token.
API Request with Token:
POST /payments
Authorization: Bearer your-jwt-access-token
Token expiration and renewal
JWTs are time-limited and will expire after a certain period. If you receive a 401 Unauthorized response, your token may have expired. To renew your token you will need to request a new one with your client_id and client_secret.
Your application should handle token expiration and request a new token before it expires. We recommend ensuring a token has at least 10 seconds of validity prior to performing any action
Unauthorized vs forbidden access
All our endpoints can return 401 Unauthorized, 403 Forbidden or 404 Not Found
401 will be returned when:
- The client credentials provided to authenticate to the
/auth/tokenendpoint are incorrect - The token provided to the any other endpoint is unknown/expired
403 will be returned when:
- The token used is not allowed to interact with the specific endpoint
404 will be returned when:
- The token used is not allowed to interact with the specific resource to due ownership mismatch.