Exchange authorization codes or refresh access tokens across all Google…
Exchange authorization codes or refresh access tokens across all Google services (Drive, Docs, Sheets, Gmail, etc.)
Request
- x-api-key: <your-api-key>
- Content-Type: application/json
Response
{
"accessToken": "<token>",
"expiresIn": 7,
"refreshToken": "<token>",
"scope": "<scope>",
"status": "success",
"tokenType": "standard",
"tokensDeducted": 1
}What it does
Exchange authorization codes or refresh access tokens across all Google services (Drive, Docs, Sheets, Gmail, etc.)
Request
Body
| Field | Type | Required | Description |
|---|---|---|---|
clientId | string | yes | |
clientSecret | string | yes | |
code | string | no | |
grantType | enum | yes | One of: authorization_code, refresh_token. |
redirectUri | string | no | |
refreshToken | string | no |
Example request
{
"clientId": "cli_804908",
"clientSecret": "<secret>",
"code": "ABC-123",
"grantType": "authorization_code",
"redirectUri": "https://example.com/page",
"refreshToken": "<token>"
}Response
| Field | Type | Description |
|---|---|---|
accessToken | string | |
expiresIn | integer | |
refreshToken | string | |
scope | string | |
status | string | |
tokenType | string | |
tokensDeducted | integer |
Example response
{
"accessToken": "<token>",
"expiresIn": 7,
"refreshToken": "<token>",
"scope": "<scope>",
"status": "success",
"tokenType": "standard",
"tokensDeducted": 1
}Errors
Every error answers with the same envelope: an HTTP status, a machine code and a sentence.
400 | The body or a parameter failed validation. The details array names the field. |
401 | No key, a revoked key or a wrong header. Send x-api-key. |
402 | The wallet cannot cover this call. Top up or wait for the monthly grant. |
404 | The resource does not exist, or it is not yours. Both answer identically and cost nothing. |
429 | Over the per-minute quota of your tier. Back off and retry. |
5xx | Upstream refused or timed out. The call is not billed. |
Code
curl -X POST 'https://app.castomapi.com/v1/google/auth/token' \
-H 'x-api-key: <your-api-key>' \
-H 'Content-Type: application/json' \
-d '{"clientId": "cli_804908", "clientSecret": "<secret>", "code": "ABC-123", "grantType": "authorization_code", "redirectUri": "https://example.com/page", "refreshToken": "<token>"}'const res = await fetch('https://app.castomapi.com/v1/google/auth/token', {
method: 'POST',
headers: { 'x-api-key': '<your-api-key>', 'Content-Type': 'application/json' },
body: JSON.stringify({"clientId": "cli_804908", "clientSecret": "<secret>", "code": "ABC-123", "grantType": "authorization_code", "redirectUri": "https://example.com/page", "refreshToken": "<token>"})
});
const data = await res.json();
console.log(data);import { request } from 'node:https';
// Node 18+ has fetch built in
const res = await fetch('https://app.castomapi.com/v1/google/auth/token', {
method: 'POST',
headers: { 'x-api-key': '<your-api-key>', 'Content-Type': 'application/json' },
body: JSON.stringify({"clientId": "cli_804908", "clientSecret": "<secret>", "code": "ABC-123", "grantType": "authorization_code", "redirectUri": "https://example.com/page", "refreshToken": "<token>"})
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
'https://app.castomapi.com/v1/google/auth/token',
headers={'x-api-key': '<your-api-key>'},
json={"clientId": "cli_804908", "clientSecret": "<secret>", "code": "ABC-123", "grantType": "authorization_code", "redirectUri": "https://example.com/page", "refreshToken": "<token>"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://app.castomapi.com/v1/google/auth/token');
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['x-api-key: <your-api-key>', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode(['clientId' => 'cli_804908', 'clientSecret' => '<secret>', 'code' => 'ABC-123', 'grantType' => 'authorization_code', 'redirectUri' => 'https://example.com/page', 'refreshToken' => '<token>']),
]);
$data = json_decode(curl_exec($ch), true);
print_r($data);Cost and limits
This operation costs 1 token. A call that fails is never billed. Every account gets 1000 free tokens a month; paid balances never expire.
Rate limits per minute
| No key | 10 |
| Free | 60 |
| Pro | 300 |
| Enterprise | 1200 |
Questions
What does Exchange authorization codes or refresh access tokens across all Google… cost?
1 token per successful call, debited from your wallet after the response. Failed calls are free. Every account receives 1000 free tokens a month.
How do I authenticate?
Send your key in the x-api-key header. One key covers every metered endpoint; there is nothing to enable per endpoint.
Can I send an Authorization: Bearer token instead?
Not for a metered endpoint — those take the x-api-key header and nothing else. Bearer tokens belong to the account side: the dashboard and account APIs accept a JWT session, and castomapi-auth issues those through a standard OAuth2 handshake, with authorization code, client credentials and refresh grants. The auth service page covers it.
What are the limits?
10 requests a minute per IP with no key at all, 60 a minute on the free tier, 300 on Pro, 1200 on Enterprise. Above the no-key tier the quota is per account, across every endpoint.
OpenAPI: https://app.castomapi.com/docs/json