# Token Authentication

#### Application Scenarios

If you use a token for authentication, you must obtain the user's token and add X-Auth-Token to the request message header of the service API when making an API call.

This section describes how to make an API call for token authentication.

#### Invoking an API

1. Obtain the following information:

   1. To obtain the IAM endpoint and region name in the message body, see Regions and Endpoints.
   2. To obtain the project ID, see Obtaining a Project ID.
2. Send a POST https://IAM_Endpoint/v3/auth/tokens request to obtain the user token.

   Table 1 Header description
   Name | Description | Mandatory | Example
   --- | --- | --- | ---
   Content-Type | Specifies the MIME type of the request body. | Yes | application/json

   An example request message is as follows:

       {
           "auth": {
               "identity": {
                   "methods": [
                       "password"
                   ],
                   "password": {
                       "user": {
                           "name": "username",
                           "password": "password",
                           "domain": {
                               "name": "domainname"
                           }
                       }
                   }
               },
               "scope": {
                   "project": {
                      "id": "project_id"
                    }
               }
           }
       }

   In the preceding command, replace parameter in italic with the actual values. For details, see the "Obtaining a User Token" section in the Identity and Access Management API Reference.

   1. IAM_Endpoint: Replace it with the IAM endpoint obtained in 1.
   2. username and password: Replace them respectively with the username and password of the IAM server.
   3. project_id: Replace it with the project ID obtained in 1.

   After the request is processed, the value of X-Subject-Token in the header is the token value.

       X-Subject-Token:MIIDkgYJKoZIhvcNAQcCoIIDgzCCA38CAQExDTALBglghkgBZQMEAgEwgXXXXX...

3. Run the following command to set the token as an environment variable for subsequent operations:

   export Token={X-Subject-Token}

   X-Subject-Token: Replace it with the token obtained in 2. An example command is as follows:

   export Token=MIIDkgYJKoZIhvcNAQcCoIIDgzCCA38CAQExDTALBglghkgBZQMEAgEwgXXXXX...

Parent topic: Environment Preparations