API reference / Authorization
Generate an access token
GET
/oauth/v1/generateChecked against the Safaricom sandbox on 1 December 2026
Returns a bearer token for the other Daraja APIs. Tokens last about one hour
(expires_in is a string of seconds). Cache the token and reuse it until it is
close to expiry instead of requesting one per call.
In the SDKs
| SDK | Method |
|---|---|
| TypeScript | accessToken() |
| Python | access_token() |
| PHP | accessToken() |
| Kotlin | accessToken() |
| Swift | accessToken() |
Request
| Environment | URL |
|---|---|
| Sandbox | https://sandbox.safaricom.co.ke/oauth/v1/generate |
| Production | https://api.safaricom.co.ke/oauth/v1/generate |
Authenticate with HTTP Basic: your consumer key and secret. The query string must be grant_type=client_credentials.
Example
curl -u "$DARAJA_CONSUMER_KEY:$DARAJA_CONSUMER_SECRET" \
"https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"Response
A new access token.
| Field | Type | Description |
|---|---|---|
access_token | stringRequired | Bearer token for the other Daraja APIs. |
expires_in | stringRequired | Seconds until the token expires, as a string. Matches ^[0-9]+$. |
Errors
| Status | Meaning |
|---|---|
400 | Daraja rejected the request. An HTTP 401 with "Invalid API call as no apiproduct match found" means your Daraja app does not have this API product enabled: add it to the app on the Daraja portal, then request a new access token. |
401 | Daraja rejected the request. An HTTP 401 with "Invalid API call as no apiproduct match found" means your Daraja app does not have this API product enabled: add it to the app on the Daraja portal, then request a new access token. |
See errors and rate limits for what to do with each.
Example response
{
"access_token": "c9SQxWWhmdVRlyh0zh8gZDTkubVF",
"expires_in": "3599"
}