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, likemyapp://callback — useful for native applications. See Redirect URIs for the scheme rules.
How PKCE works:
- The client generates a
code_verifier: a random string of 43 to 128 characters from[A-Z] [a-z] [0-9] - . _ ~. - The client derives a
code_challengefrom thecode_verifierusing the method incode_challenge_method:S256—code_challengeis the Base64 URL-encoded SHA-256 hash ofcode_verifier. Use this method.plain—code_challengeequalscode_verifier. Used ifcode_challenge_methodis omitted.
- The client sends
code_challengeandcode_challenge_methodin the authorization request. - The server returns the authorization
code. - The client sends the original
code_verifierwhen exchanging the code for a token. - The server checks
code_verifieragainst thecode_challengefrom step 3 and, if it matches, returns theaccess_token.
code_verifier and code_challenge in the browser with the Web Crypto API:
Generate a PKCE code_verifier and code_challenge
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:roto list chats). See OAuth scopes for the full list. - Client type — Web app or Server-side app, depending on where your app runs.
Step 2: Redirect users to the Text OAuth server
Redirect users to:Authorization request with PKCE
prompt=consent to trigger this screen again for testing.
Step 3: Acquire the code
After the user clicks Allow, Text redirects them back to yourredirect_uri with a code parameter:
Example redirection back to the app
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:Response
Step 5: Use the token in API calls
Useaccess_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:roscope (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
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
Whenaccess_token expires, exchange refresh_token for a new one with an HTTP POST request: