> ## Documentation Index
> Fetch the complete documentation index at: https://developers.telnyx.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OAuth token endpoint

> Exchange authorization code, client credentials, or refresh token for access token



## OpenAPI

````yaml https://telnyx-openapi-ng.s3.us-east-1.amazonaws.com/oauth.yml post /oauth/token
openapi: 3.1.0
info:
  title: Telnyx OAuth API
  version: 2.0.0
  description: API for OAuth.
  contact:
    email: support@telnyx.com
servers:
  - url: https://api.telnyx.com/v2
security:
  - bearerAuth: []
paths:
  /oauth/token:
    post:
      tags:
        - OAuth Protocol
      summary: OAuth token endpoint
      description: >-
        Exchange authorization code, client credentials, or refresh token for
        access token
      operationId: ExchangeOAuthToken
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/TokenRequest'
          application/json:
            schema:
              $ref: '#/components/schemas/TokenRequest'
      responses:
        '200':
          description: Token response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TokenResponse'
        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/oauth_Error'
              examples:
                invalid_request:
                  value:
                    error: invalid_request
                invalid_grant:
                  value:
                    error: invalid_grant
                unsupported_grant_type:
                  value:
                    error: unsupported_grant_type
                invalid_scope:
                  value:
                    error: invalid_scope
        '401':
          description: Invalid client credentials
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/oauth_Error'
              example:
                error: invalid_client
      security:
        - oauthClientAuth: []
        - {}
      x-codeSamples:
        - lang: JavaScript
          source: >-
            import Telnyx from 'telnyx';


            const client = new Telnyx();


            const response = await client.oauth.token({ grant_type:
            'client_credentials' });


            console.log(response.access_token);
        - lang: Python
          source: |-
            from telnyx import Telnyx

            client = Telnyx()
            response = client.oauth.token(
                grant_type="client_credentials",
            )
            print(response.access_token)
        - lang: Go
          source: "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\t\"github.com/team-telnyx/telnyx-go\"\n\t\"github.com/team-telnyx/telnyx-go/option\"\n)\n\nfunc main() {\n\tclient := telnyx.NewClient(\n\t\toption.WithClientID(\"My Client ID\"),\n\t\toption.WithClientSecret(\"My Client Secret\"),\n\t)\n\tresponse, err := client.OAuth.Token(context.TODO(), telnyx.OAuthTokenParams{\n\t\tGrantType: telnyx.OAuthTokenParamsGrantTypeClientCredentials,\n\t})\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\tfmt.Printf(\"%+v\\n\", response.AccessToken)\n}\n"
        - lang: Java
          source: |-
            package com.telnyx.sdk.example;

            import com.telnyx.sdk.client.TelnyxClient;
            import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;
            import com.telnyx.sdk.models.oauth.OAuthTokenParams;
            import com.telnyx.sdk.models.oauth.OAuthTokenResponse;

            public final class Main {
                private Main() {}

                public static void main(String[] args) {
                    TelnyxClient client = TelnyxOkHttpClient.fromEnv();

                    OAuthTokenParams params = OAuthTokenParams.builder()
                        .grantType(OAuthTokenParams.GrantType.CLIENT_CREDENTIALS)
                        .build();
                    OAuthTokenResponse response = client.oauth().token(params);
                }
            }
        - lang: Ruby
          source: >-
            require "telnyx"


            telnyx = Telnyx::Client.new(client_id: "My Client ID",
            client_secret: "My Client Secret")


            response = telnyx.oauth.token(grant_type: :client_credentials)


            puts(response)
        - lang: PHP
          source: |-
            <?php

            require_once dirname(__DIR__) . '/vendor/autoload.php';

            use Telnyx\Client;
            use Telnyx\Core\Exceptions\APIException;

            $client = new Client(
              clientID: getenv('TELNYX_CLIENT_ID') ?: 'My Client ID',
              clientSecret: getenv('TELNYX_CLIENT_SECRET') ?: 'My Client Secret',
            );

            try {
              $response = $client->oauth->token(
                grantType: 'client_credentials',
                clientID: 'client_id',
                clientSecret: 'client_secret',
                code: 'code',
                codeVerifier: 'code_verifier',
                redirectUri: 'https://example.com',
                refreshToken: 'refresh_token',
                scope: 'admin',
              );

              var_dump($response);
            } catch (APIException $e) {
              echo $e->getMessage();
            }
        - lang: CLI
          source: |-
            telnyx oauth token \
              --client-id 'My Client ID' \
              --client-secret 'My Client Secret' \
              --grant-type client_credentials
components:
  schemas:
    TokenRequest:
      type: object
      properties:
        grant_type:
          type: string
          enum:
            - client_credentials
            - authorization_code
            - refresh_token
          description: OAuth 2.0 grant type
        scope:
          type: string
          description: Space-separated list of requested scopes (for client_credentials)
          example: admin
        code:
          type: string
          description: Authorization code (for authorization_code flow)
        redirect_uri:
          type: string
          format: uri
          description: Redirect URI (for authorization_code flow)
        code_verifier:
          type: string
          description: PKCE code verifier (for authorization_code flow)
        refresh_token:
          type: string
          description: Refresh token (for refresh_token flow)
        client_id:
          type: string
          description: OAuth client ID (if not using HTTP Basic auth)
        client_secret:
          type: string
          description: OAuth client secret (if not using HTTP Basic auth)
      required:
        - grant_type
    TokenResponse:
      type: object
      properties:
        access_token:
          type: string
          description: The access token
        token_type:
          type: string
          enum:
            - Bearer
          description: Token type
        expires_in:
          type: integer
          description: Token lifetime in seconds
        scope:
          type: string
          description: Space-separated list of granted scopes
        refresh_token:
          type: string
          description: Refresh token (if applicable)
      required:
        - access_token
        - token_type
        - expires_in
    oauth_Error:
      type: object
      properties:
        error:
          type: string
          description: Error code
        error_description:
          type: string
          description: Human-readable error description
      required:
        - error
  securitySchemes:
    bearerAuth:
      scheme: bearer
      type: http

````