Skip to main content
The authorization code grant works the same way for both client types: the user is redirected to the Text OAuth server, and after they authorize your app, they’re redirected back with a single-use authorization code. Your app then exchanges that code for an access_token and a refresh_token.
  • Server-side apps authenticate this exchange with their Client Secret. They can also add PKCE, but PKCE doesn’t replace the Client Secret.
  • Web apps can’t hold a Client Secret, so PKCE is required instead.

PKCE extension

OAuth 2.1 requires the PKCE (Proof Key for Code Exchange) extension for public clients using the authorization code grant. It also enables Web app clients to use a custom-scheme redirect URI, like myapp://callback — useful for native applications. See Redirect URIs for the scheme rules. How PKCE works:
  1. The client generates a code_verifier: a random string of 43 to 128 characters from [A-Z] [a-z] [0-9] - . _ ~.
  2. The client derives a code_challenge from the code_verifier using the method in code_challenge_method:
    • S256 — code_challenge is the Base64 URL-encoded SHA-256 hash of code_verifier. Use this method.
    • plain — code_challenge equals code_verifier. Used if code_challenge_method is omitted.
  3. The client sends code_challenge and code_challenge_method in the authorization request.
  4. The server returns the authorization code.
  5. The client sends the original code_verifier when exchanging the code for a token.
  6. The server checks code_verifier against the code_challenge from step 3 and, if it matches, returns the access_token.
Generate code_verifier and code_challenge in the browser with the Web Crypto API:
Generate a PKCE code_verifier and code_challenge
Store codeVerifier until you exchange the code — you’ll send it as code_verifier in Step 4. Don’t send it in the authorization request.

Implementing the authorization code grant

Step 1: Create an OAuth client

Go to Settings → API access → OAuth clients and create a new OAuth client. Configure:
  • Display name — a name to identify your OAuth client.
  • Redirect URI whitelist — where users are redirected after authorization. See Redirect URIs for the rules governing this value.
  • Access scopes — the scopes your app needs (for example, chats--all:ro to list chats). See OAuth scopes for the full list.
  • Client type — Web app or Server-side app, depending on where your app runs.
After creation, Text shows your Client ID and, for server-side apps, your Client Secret once. Copy both immediately — the secret isn’t shown again.
Never include a Client Secret in browser code, a mobile app, a public repository, or a URL. Anyone who can read it can impersonate your OAuth client.

Step 2: Redirect users to the Text OAuth server

Redirect users to:
Request parameters
Authorization request with PKCE
At this point, the user is asked to allow your app to access the resources covered by your client’s configured scopes. Use prompt=consent to trigger this screen again for testing.

Step 3: Acquire the code

After the user clicks Allow, Text redirects them back to your redirect_uri with a code parameter:
Example redirection back to the app
The code is single-use and valid for only a few minutes. Compare the returned state with the value you sent in Step 2, and reject the request if they don’t match, before exchanging the code. If authorization fails, Text redirects back with error and, optionally, error_description instead of code. See Authorization errors for the possible values.

Step 4: Exchange the code for tokens

Make an HTTP POST request to:
Text accepts this request as either form-encoded or JSON. Request parameters
Response
Response

Step 5: Use the token in API calls

Use access_token to authorize requests to Text APIs. Store the token securely until it expires — caching it avoids unnecessary redirects to the Text OAuth server.

Practical example: listing chats

This example walks through the authorization code grant to call the List Chats method from the Agent Chat API. Before you start, make sure you have:
  • A Text Owner or Admin account.
  • A Server-side app OAuth client configured with the chats--all:ro scope (see Step 1) — this example exchanges the code with a Client Secret.
  • A tool to send requests, such as cURL or Postman.
1

Get your code

Open this URL in your browser while logged in, replacing the placeholders with your own values:
After you authorize the request, Text redirects you to your redirect URI with a code parameter:
2

Exchange the code for a token

The response includes access_token, refresh_token, and the other fields described in Step 4.
3

Call the API

Call List Chats, replacing the placeholder token with your access_token:

Using the refresh token

When access_token expires, exchange refresh_token for a new one with an HTTP POST request:
Request parameters
For Web app clients, refresh_token rotates: every successful refresh invalidates the previous refresh token and returns a new one. Always save the refresh_token from the response, or the next refresh will fail. Server-side app refresh tokens don’t rotate.
The response has the same shape as the code exchange response in Step 4.