GladysDevelopers

Authentication

Exchange your app's keys for an access token, and send it with every request.

Gladys uses OAuth 2.0. Your server exchanges its app's client ID and client secret for a short-lived access token, and sends that token with every API request:

Authorization: Bearer access_…

Getting a token

Use the client credentials grant. Send the parameters form-encoded, with the secret in the body:

curl https://api.joingladys.com/api/v1/oauth/token \
  -d grant_type=client_credentials \
  -d client_id=client_test_… \
  -d client_secret=secret_test_…
{
  "access_token": "access_…",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "payments:create payments:read payments:capture payments:refund"
}

Prop

Type

The token lasts expires_in seconds, an hour by default. There is no refresh token for this grant: when a token expires, ask for a new one the same way. A simple approach is to keep the token in memory and fetch a new one when a request comes back 401.

The secret goes in the request body. HTTP Basic authentication is not supported.

Keep your secret on your server

Anyone with your client secret can act as your app. Never put it in a browser or mobile app, and never commit it to source control. The API only accepts browser requests from Gladys's own sites, so calls have to come from your server anyway.

Errors

Token errors use the standard OAuth shape, so OAuth libraries understand them:

{ "error": "invalid_client", "error_description": "client authentication failed" }
errorStatusMeans
invalid_request400A required parameter is missing.
invalid_client401Unknown client ID, wrong secret, or the app is deactivated.
unauthorized_client400Your app isn't registered for this grant. Payments apps need Server-to-server (client credentials).
unsupported_grant_type400grant_type is not one Gladys supports.
invalid_scope400You asked for a scope your app doesn't hold.
server_error500Something went wrong on our side. Try again.

Scopes

A token can only do what its scopes allow. Choose them when you create the app.

ScopeLets your app
payments:createCreate payments.
payments:readRead payments and refunds; list webhook endpoints and deliveries.
payments:captureCapture and cancel payments; manage webhook endpoints and replay deliveries.
payments:refundRefund payments.

A request without the scope it needs gets 403.

Your keys

  • Client IDs start client_test_ or client_live_, and secrets secret_test_ or secret_live_, so you can always tell which environment a key belongs to.
  • The secret is shown once, when the app is created. Gladys stores only a hash of it.
  • Regenerate secret on the app's page issues a new one, and the old one stops working immediately: token requests fail until your server has the new secret. Access tokens already issued keep working until they expire, which usually covers the time it takes to deploy it.

Revoking a token

To end a token before it expires, for example when rotating keys:

curl https://api.joingladys.com/api/v1/oauth/revoke \
  -d token=access_… \
  -d client_id=client_test_… \
  -d client_secret=secret_test_…

It answers 200 whether or not the token existed.

Using an OAuth library

Gladys publishes its OAuth configuration at /.well-known/oauth-authorization-server, so most OAuth client libraries can configure themselves from https://api.joingladys.com. For client credentials you only need the token endpoint, https://api.joingladys.com/api/v1/oauth/token, and to send the secret in the request body (sometimes called client_secret_post).

// Go: golang.org/x/oauth2/clientcredentials
config := clientcredentials.Config{
	ClientID:     os.Getenv("GLADYS_CLIENT_ID"),
	ClientSecret: os.Getenv("GLADYS_CLIENT_SECRET"),
	TokenURL:     "https://api.joingladys.com/api/v1/oauth/token",
	AuthStyle:    oauth2.AuthStyleInParams,
}
client := config.Client(ctx) // fetches and renews tokens for you

On this page