daraja-openapi

API reference / Authorization

Generate an access token

GET/oauth/v1/generate

Checked 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

SDKMethod
TypeScriptaccessToken()
Pythonaccess_token()
PHPaccessToken()
KotlinaccessToken()
SwiftaccessToken()

Request

EnvironmentURL
Sandboxhttps://sandbox.safaricom.co.ke/oauth/v1/generate
Productionhttps://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.

FieldTypeDescription
access_tokenstringRequiredBearer token for the other Daraja APIs.
expires_instringRequiredSeconds until the token expires, as a string. Matches ^[0-9]+$.

Errors

StatusMeaning
400Daraja 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.
401Daraja 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"
}

View the source of this page

Built by ByteGigs Labs. Apache 2.0. M-PESA, Daraja and Safaricom are trademarks of Safaricom PLC; this project is independent and not endorsed by Safaricom.