Get an access token
/oauth2/tokenReturns a bearer token for your OAuth 2.0 client. Send it in the Authorization header of every other request, in place of the apiKey header. It lasts an hour.
Create the client under Settings › Developers › API Keys in the app. A Test client's tokens work only at the sandbox base URL, and a Live client's only at the live one. Get the token from the base URL you'll call. See Use OAuth 2.0 instead.
Headers
- Basic, then your client ID and client secret joined by a colon and Base64-encoded. curl and most HTTP libraries build it for you from the two values.Example:
Basic <Base64 of clientId:clientSecret> - The body is a form, not JSON.Example:
application/x-www-form-urlencoded
Body parameters
- Always client_credentials.Example:
client_credentials
Use the token
curl $PONITOR_BASE_URL/messaging/v1/sms/e2c38865-dc0a-4299-baef-c07a9bfa9c33 \
-H "Authorization: Bearer $PONITOR_ACCESS_TOKEN"
Keep the token and reuse it until expires_in runs out, or until a call answers 401. Then get a new one. Getting a token for every call only slows your calls down.
Example request
- curl
- Node.js
- Python
- PHP
curl -X POST "https://api.ponitor.com/oauth2/token" \
-u "$PONITOR_CLIENT_ID:$PONITOR_CLIENT_SECRET" \
-d grant_type=client_credentials
const basic = Buffer.from(
`${process.env.PONITOR_CLIENT_ID}:${process.env.PONITOR_CLIENT_SECRET}`,
).toString('base64');
const response = await fetch('https://api.ponitor.com/oauth2/token', {
method: 'POST',
headers: {
Authorization: `Basic ${basic}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({ grant_type: 'client_credentials' }),
});
console.log(response.status, await response.json());
import os
import requests
response = requests.post(
"https://api.ponitor.com/oauth2/token",
auth=(os.environ["PONITOR_CLIENT_ID"], os.environ["PONITOR_CLIENT_SECRET"]),
data={"grant_type": "client_credentials"},
timeout=30,
)
print(response.status_code, response.json())
<?php
$ch = curl_init('https://api.ponitor.com/oauth2/token');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_USERPWD => getenv('PONITOR_CLIENT_ID') . ':' . getenv('PONITOR_CLIENT_SECRET'),
CURLOPT_POSTFIELDS => http_build_query(['grant_type' => 'client_credentials']),
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
echo $status, ' ', $body, PHP_EOL;
Responses
- 200
- 400
- 401
OK. The token, and how many seconds it lasts.
{
"access_token": "<your access token>",
"scope": "default",
"token_type": "Bearer",
"expires_in": 3600
}
Bad Request. grant_type is missing, or isn't client_credentials.
{
"error": "unsupported_grant_type",
"error_description": "Unsupported grant_type value"
}
This call's errors are OAuth 2.0's, with error and error_description, not Ponitor's error object.
Unauthorized. The client ID or secret is wrong, or the secret was replaced with a new one.
{
"error": "invalid_client",
"error_description": "Client Authentication failed."
}