Create an API key

Updated at:

Create an API Key for a RAM user by using the RAM console or by calling an OpenAPI operation.

An API Key belongs to a specific RAM user and is bound to a single cloud service. The plaintext value is displayed only once upon creation. Copy the plaintext or download the CSV file to save it immediately.

Prerequisites

  • A RAM user must already exist. If not, see Create a RAM user.

  • You must have the required permissions to manage API Keys, which vary by use case:

    • To create an API Key for yourself, you must meet the following conditions:

      1. Your Alibaba Cloud account (root account) must be configured to allow RAM users to manage their own API Keys. To enable this setting:

        • In the console: In the RAM console , navigate to Security Settings and enable Allow RAM users to manage their own API Keys.

        • By API: Call the SetSecurityPreference operation and set the AllowUserToManageServiceCredential parameter to true.

      2. The RAM user must not be restricted by a permission policy that contains a Deny statement for API Key-related operations.

    • To create an API Key for another user, you must have a permission policy that includes the ram:CreateServiceCredential action. To limit permissions to a specific cloud service, you can use the ram:ServiceCredentialServiceName condition key in the policy.

  • The target cloud service must support API Keys. You can check supported cloud services in the following ways:

    • In the console: Log on to the RAM console . On the user details page, go to the Credentials tab. In the API Keys section, click Create API Key. In the dialog box that appears, view the available cloud services.

    • By API: Call the ListServiceCredentialServices operation.

  • The target RAM user has not reached the API Key quota for the specified cloud service. Each RAM user can have a maximum of two API Keys per cloud service. If the quota is reached, delete an existing API Key before creating a new one.

Via RAM console

  1. Log on to the RAM console with your Alibaba Cloud account or a RAM user that has the required permissions.

  2. In the left-side navigation pane, navigate to Identities > Users. In the user list, click the logon name of the target RAM user.

  3. Click the Credentials tab. In the API Keys section, click Create API Key.

  4. In the Create API Key dialog box, configure the following parameters:

    Parameter

    Required

    Description

    Name

    Yes

    A name to identify the purpose of the API Key. The system automatically generates a default name in the format of {cloud-service-name}_apikey_{random-characters}, which you can modify. The name must be unique for the RAM user.

    Cloud Service

    Yes

    Select a cloud service from the drop-down list. If the user has reached the quota for a specific service, that option is disabled.

    Expiration Time

    Yes

    Select 1 Day, 7 Days, 30 Days, 90 Days, or Never expires. You can also select Custom and enter a value from 1 to 36,600 days. If you enter a value outside this range, such as 36,601, the Create button is disabled.

  5. Click Create. The Save API Key dialog box appears, displaying the ID, name, expiration time, and the plaintext API Key.

  6. Click Copy next to the API Key field or Download CSV to download the credential file. Store the credential in a secure location.

    Warning

    The plaintext API Key is displayed only once. After this dialog box is closed, the key cannot be retrieved. If you close the dialog box without saving the key, you must delete this credential and create a new one.

  7. Click Close. The new API Key appears in the list with a status of Active.

Via OpenAPI

Call the CreateServiceCredential operation to create a service credential (API Key).

Request parameters

Parameter

Type

Required

Description

ServiceName

String

Yes

The name of the Alibaba Cloud service. Example: ak.aliyuncs.com.

UserPrincipalName

String

Yes

The logon name of the RAM user. Format: usern***@AccountID.onaliyun.com.

ServiceCredentialName

String

Yes

The name of the service credential. This name helps you identify the credential and must be unique for the RAM user.

CredentialAgeDays

String

No

The validity period of the credential, in days. Valid values: 1 to 36,600. If you do not specify this parameter, the credential never expires (ExpirationTime is null).

Response parameters

The operation returns a RequestId and a ServiceCredential object with the following fields:

  • ServiceName: The name of the Alibaba Cloud service.

  • UserPrincipalName: The logon name of the RAM user.

  • ServiceCredentialName: The name of the service credential.

  • ServiceCredentialId: The ID of the service credential (prefixed with SC).

  • ServiceCredentialSecret: The plaintext API Key. This value is returned only once, during creation. You must save it immediately in a secure location.

  • CreateTime: The creation time.

  • ExpirationTime: The expiration time. This is null if the key never expires.

  • Status: The status of the credential. After creation, the status is Active.

Notes

  • The plaintext API Key (ServiceCredentialSecret) is returned only upon creation. It is not included in the response of subsequent calls to the ListServiceCredentials or GetServiceCredential operations, nor is it visible in the console.

  • Each RAM user can have a maximum of two API Keys per cloud service, including both active and inactive keys. If this quota is reached, the corresponding option in the Cloud Service drop-down list is disabled. Delete an existing credential for that service before creating a new one.

  • The cloud service scope of an API Key cannot be modified after creation. To change the cloud service, create a new API Key and delete the original one.

  • You cannot create an API Key for an Alibaba Cloud account (root account).