Autentikasi & Kredensial
Generate B2B Token
B2B (Business-to-Business) Token diperlukan untuk autentikasi setiap request ke SNAP API.
POST
https://staging-gateway.paprika.co.id/api/snap/v1.0/access-token/b2bB2B (Business-to-Business) Token diperlukan untuk autentikasi setiap request ke SNAP API. Kirim grantType: "client_credentials" pada request body, lalu gunakan kembali accessToken yang dikembalikan selama token masih berlaku (15 menit).
Contoh Implementasi
const axios = require("axios");
const crypto = require("crypto");
async function getB2BToken() {
const clientKey = process.env.SNAP_CLIENT_KEY;
const clientSecret = process.env.SNAP_CLIENT_SECRET;
const timestamp = new Date().toISOString();
// Generate signature
const stringToSign = clientKey + "|" + timestamp;
const signature = crypto
.createHmac("sha256", clientSecret)
.update(stringToSign)
.digest("base64");
try {
const response = await axios.post(
"https://staging-gateway.paprika.co.id/api/snap/v1.0/access-token/b2b",
{
grantType: "client_credentials",
},
{
headers: {
"Content-Type": "application/json",
"X-CLIENT-KEY": clientKey,
"X-TIMESTAMP": timestamp,
"X-SIGNATURE": signature,
},
},
);
return response.data;
} catch (error) {
console.error(
"Error getting B2B token:",
error.response?.data || error.message,
);
throw error;
}
}function getB2BToken() {
$clientKey = getenv('SNAP_CLIENT_KEY');
$clientSecret = getenv('SNAP_CLIENT_SECRET');
$timestamp = date('c'); // ISO 8601 format
// Generate signature
$stringToSign = $clientKey . '|' . $timestamp;
$signature = base64_encode(hash_hmac('sha256', $stringToSign, $clientSecret, true));
$ch = curl_init('https://staging-gateway.paprika.co.id/api/snap/v1.0/access-token/b2b');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(['grantType' => 'client_credentials']),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-CLIENT-KEY: ' . $clientKey,
'X-TIMESTAMP: ' . $timestamp,
'X-SIGNATURE: ' . $signature
]
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
return json_decode($response, true);
} else {
throw new Exception('Failed to get B2B token: ' . $response);
}
}Parameters
4 parameters accepted on this request.
| Name | Type | In | Required | Description |
|---|---|---|---|---|
| string | header | Required | application/json | |
| string | header | Required | Client key merchant (MERCHANT_API_KEY). | |
| datetime | header | Required | YYYY-MM-DDTHH:mm:ssZ | |
| string | header | Required | Asymmetric signature dari clientKey|timestamp. |
Request
Content-Type application/json
{
"grantType": "client_credentials"
}Response Samples
200 OKSuccessful
{
"responseCode": "2007100",
"responseMessage": "Successful",
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"tokenType": "Bearer",
"expiresIn": 900
}401 UnauthorizedUnauthorized
{
"responseCode": "4017100",
"responseMessage": "Unauthorized. [Invalid Signature]"
}