Daraja SDK Logomark
Daraja SDK
Authentication

Authorization

Generate the OAuth access token every other Daraja call requires.

Safaricom documents this as the API that must be called before any other API on the platform, since every other endpoint requires the access token it returns.

Endpoint

GET https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials

Overview

  • OAuth 2.0, client credentials grant.
  • Tokens expire after 3600 seconds (1 hour).
  • The Daraja Simulator generates and refreshes tokens automatically when you pick an app to simulate with, so you only need to implement this yourself outside the simulator.

Prerequisites

  • A Daraja account on the Safaricom Developer Portal.
  • A sandbox app created on the portal, which supplies a Consumer Key and Consumer Secret.

Get API Credentials

Retrieve your Consumer Key and Consumer Secret from My Apps on the portal.

Keep credentials strictly secret

Never expose or commit your Consumer Key and Consumer Secret in client-side code, frontend bundles, or public repositories. Treat them as confidential server-side secrets at all times.

Send Basic Auth Request

Send a GET request to the endpoint above using HTTP Basic Authentication, with the Consumer Key as the username and the Consumer Secret as the password.

Receive Access Token

Safaricom validates the credentials and returns an access token.

Attach Bearer Token

Use that token as a Bearer token on every subsequent API call, until it expires.

Request

Documented as an "AUTHORIZATION (Basic Auth)" request with username and password, sent as the Basic Auth header:

Authorization: Basic Q2RtTmJkdDBpQk4xb3FEZkthc200ZGFiZHBLbXRhTm46RExLRzdQQnVuNzIwR1ppbQ==

Prop

Type

Response

{
  "access_token": "c9SQxWWhmdVRlyh0zh8gZDTkubVF",
  "expires_in": 3599
}
FieldDescriptionSample value
access_tokenThe Bearer token to use on subsequent calls.c9SQxWWhmdVRlyh0zh8gZDTkubVF
expires_inSeconds until the token expires.3599

Error responses

Error codeMeaningCauseFix
400.008.02Invalid grant typeWrong value in grant_typeUse client_credentials.
400.008.01Invalid authentication typeAuthorization header isn't BasicUse Basic Auth.

Testing

Use the Authorization API Simulator on the portal: pick one of your apps, let it invoke the request, and check the response. Outside the simulator, generate the token yourself against the sandbox endpoint above and reuse it (before it expires) across whichever product you're testing.

On this page