Skip to main content

Authentication

This guide explains how to use OAuth 2.0 Authorization Code to let users grant your app access to their CleverReach account. OAuth authorizes API access. It does not sign users in to your app.

For a detailed OAuth explanation please have a look at this introduction to OAuth.

OAuth at a glance

OAuth connects a CleverReach account to your app without sharing the user's password:

  • The user signs in at CleverReach and approves the access your app requests.
  • CleverReach returns a short-lived authorization code. Your server exchanges it for:
    • an access token to make approved API calls
    • a refresh token to get a new access token later.
  • The approved scopes limit what your app can do. The user can disconnect at any time.
Security

Treat access and refresh tokens as credentials: store them encrypted, keep the client secret on your server, and never expose either in a browser, source repository, or log. Request only the permissions your integration needs.

OAuth Authorization Code Flow​

Create an OAuth app in CleverReach under Account > Extras > REST API and select the scopes your integration needs. See Scopes below for the available permissions. Register the callback URL where your application can receive the authorization response. Do not use the client credentials grant because it does not connect an individual user's account. See the Integration Partner Program for listing requirements.

  1. Start the authorization. Generate a cryptographically random state value and store it in the user's session. Then redirect the user to CleverReach:

    https://rest.cleverreach.com/oauth/authorize.php?client_id=<CLIENT_ID>&response_type=code&redirect_uri=<URL_ENCODED_REDIRECT_URI>&state=<STATE>

The authorization request uses response_type=code to ask for an authorization code. Choose the required permissions (scopes) when setting up the app. These scopes limit the API capabilities available to the resulting token. The redirect URI must exactly match the one registered for the app.

  1. Get the user's approval. The user signs in to CleverReach and approves the requested access. CleverReach redirects the browser to your callback with a short-lived code and the original state. If the user declines, handle the authorization error instead of expecting a code.

  2. Validate and exchange the code. The state value links the callback to the authorization request your app started and helps prevent forged callbacks. Compare the returned value with the one stored in that user's session. Reject the callback if it is missing or does not match. Immediately exchange the code server-side at https://rest.cleverreach.com/oauth/token.php using grant_type=authorization_code, the code, your app's client_id and client_secret, and the same redirect_uri. grant_type belongs to this token request, not the authorization request. If the code is missing, expired, or already used, restart authorization rather than reusing it.

  3. Call the API. Store the returned access_token and refresh_token securely and associate them with the connected CleverReach account. Include the access token in the Authorization header of API requests. When the access token expires, use the refresh token at the same token endpoint with grant_type=refresh_token, then securely store the newly returned tokens. If refreshing fails, mark the connection as needing authorization again instead of repeatedly retrying with expired credentials.

Never ask users for their CleverReach password. Do not log authorization codes, tokens, or client secrets.

Using the PHP SDK

The official CleverReach PHP SDK supports the flow through OAuthHelper:

  • Creates the authorization URL and validates state.
  • Exchanges codes and refreshes tokens automatically.
  • Revokes tokens when a user disconnects.

For multi-account apps, configure token storage to associate each user's tokens with the correct account. See the SDK's OAuth documentation for setup and examples.

Scopes​

Scopes limit what an OAuth token can do. They are permissions, not user identity or login details. Configure the required scopes for your OAuth app before sending users through authorization. The resulting token can access only the granted capabilities. Follow least privilege and add a scope only when an API operation requires it.

The backend's standard OAuth scope selection shows these six scopes:

Backend labelPresetAccess provided
Recipientsoa_receiversRecipients, groups, attributes, filters, blacklist, orders, events, and tags
Reportsoa_reportsReports
Formsoa_formsForms and Flows
Emailsoa_mailingsMailing, content, and category management. Does not include sending mailings
SSLoa_sslSSL settings
Webhookoa_webhooksWebhook management

Additional scopes are available for sending mailings, orchestrating agency subaccounts, and managing or triggering automations. Contact our support to have these scopes enabled for your app.

Disconnecting an Account​

When a user disconnects CleverReach, revoke the active token with DELETE /v3/oauth/token. The endpoint deletes the token used for the request, so this must be its last API call.

curl --request DELETE \
--url https://rest.cleverreach.com/v3/oauth/token \
--header 'Authorization: Bearer <ACCESS_TOKEN>'

After revocation succeeds:

  • Mark the connection as disconnected and remove the stored access and refresh tokens.
  • Stop scheduled jobs and webhook processing for that account.

Track connection status separately in your application instead of inferring it from a previously stored token. Do not make further API calls with the revoked token.