Managing API keys
An API key is a secret key used for simplified service account authentication in the Yandex Cloud API.
If you do not have a service account yet, create one and assign roles to it.
Creating an API key
To create a service account API key:
- In the management console
, navigate to the folder the service account belongs to. - From the list of services, select Identity and Access Management.
- In the left-hand panel, select
Service accounts. - Select the service account to create an API key for. Create a new service account if you need to.
- In the top panel, click
Create new key and select Create API key. - Enter a description of the key so that you can easily find it in the management console.
- Select Scope. For more information about scopes, see API keys with scope and validity limits.
- Optionally, specify Expires at.
- Click Create.
- Save the ID and secret key.
Alert
After you close this dialog, the key value will not be shown again.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
See the description of the create API key command:
yc iam api-key create --help
-
Get a list of service accounts in the default folder:
yc iam service-account list
Result:
+----------------------+------------------+-------------------------------+ | ID | NAME | DESCRIPTION | +----------------------+------------------+-------------------------------+ | aje6o61dvog2******** | my-robot | | | aje9sda1ufvq******** | account_name | account_description | +----------------------+------------------+-------------------------------+
-
Create an API key for the required service account and save the response to the
api_key.yaml
file:yc iam api-key create \ --service-account-name <service_account_name> \ --scope <scope> \ --expires-at <date_and_time> \ > api_key.yaml
Where:
--service-account-name
: Service account name. This is a required parameter.--scope
: Key scope. This is an optional parameter.--expires-at
: Key expiration date and time. This is an optional parameter.api_key.yaml
: File to save the response to.
As a result, you will get the
api_key.yaml
file with the API key value in thesecret
field:api_key: id: ajeefjedtpbi******** service_account_id: ajeg2b2et02f******** created_at: "2025-03-03T16:29:04.709971428Z" scope: yc.postbox.send scopes: - yc.postbox.send expires_at: "2025-04-09T08:41:27Z" secret: AQVNznzc3uVybtct16KkWUFCdQEneK2-********
Save the key value you got in a secure location. You will not be able to get it again.
If you don't have Terraform, install it and configure the Yandex Cloud provider.
-
In the Terraform configuration file, define the parameters of the resources you want to create:
resource "yandex_iam_service_account_api_key" "sa-api-key" { service_account_id = "<service_account_ID>" description = "<key_description>" scope = "<scope>" expires_at = "<date_and_time>" pgp_key = "<PGP_key>" output_to_lockbox { secret_id = "<Lockbox_secret_ID>" entry_for_secret_key = "<secret_key>" } }
Where:
service_account_id
: Service account ID. This is a required parameter.description
: Key description. This is an optional parameter.scope
: Key scope. This is an optional parameter.expires_at
: Key expiration date and time inYYYY-MM-DDThh:mm:ssZ
format. This is an optional parameter.pgp_key
: Additional PGP key for encrypting a private key. Specify the public part of the key in Base64 encoding or inkeybase:keybaseusername
format. This is an optional parameter.output_to_lockbox
: Description of the Yandex Lockbox secret to save the API key value to, in order to avoid its possible leak through theterraform.tfstate
file. This is an optional parameter. Nested parameters:secret_id
: ID of the Yandex Lockbox secret to save the API key value to. The secret must be custom.entry_for_secret_key
: Secret key to assign to the API key value being saved.
To learn more about the resources you can create with Terraform, see the Terraform documentation
. -
Create the resources:
-
In the terminal, change to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using the command:
terraform validate
If the configuration is correct, the following message is returned:
Success! The configuration is valid.
-
Run the command:
terraform plan
The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
in the terminal and press Enter.
Terraform will create all the required resources. You can see their detailed description using the management console
or this CLI command:yc iam api-key list --service-account-id <service_account_ID>
-
Use the create REST API method for the ApiKey resource:
export SERVICEACCOUNT_ID=<service_account_ID>
export IAM_TOKEN=<token>
curl \
--request POST \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $IAM_TOKEN" \
--data "{
\"serviceAccountId\": \"$SERVICEACCOUNT_ID\",
\"scope\": \"<scope>\",
\"expiresAt\": \"<date_and_time>\"
}" \
https://iam.api.cloud.yandex.net/iam/v1/apiKeys
Where:
SERVICEACCOUNT_ID
: Service account ID. This is a required parameter.IAM_TOKEN
: IAM token. This is a required parameter.scope
: Key scope. This is an optional parameter.expiresAt
: Expiration date and time for the key with restricted access. This is an optional parameter.
You can also create an API key using the ApiKeyService/Create gRPC API call.
To learn how to transmit a key in a request, read the guides for the respective services supporting this authentication method.
Viewing available scopes for an API key
To view available scopes of an API key, run this command:
yc iam api-key list-scopes
Result:
- yc.ai.foundationModels.execute
- yc.ai.imageGeneration.execute
- yc.ai.languageModels.execute
- yc.ai.speechkitStt.execute
- yc.ai.speechkitTts.execute
- yc.ai.translate.execute
- yc.ai.vision.execute
- yc.monitoring.manage
- yc.monitoring.read
- yc.postbox.send
- yc.search-api.execute
- yc.serverless.containers.invoke
- yc.serverless.functions.invoke
- yc.ydb.tables.manage
- yc.ydb.topics.manage
To view the available scopes of an API key, use the ListScopes REST API method for the ApiKey resource or the ApiKeyService/ListScopes gRPC API call.
Examples
Adding a description when creating an API key
To add an API key description when creating the key:
yc iam api-key create \
--service-account-name <service_account_name> \
--description "this API-key is for my-robot"
Where:
--service-account-name
: Service account name. This is a required parameter.--description
: API key description. This is an optional parameter.
resource "yandex_iam_service_account_api_key" "sa-api-key" {
service_account_id = "<service_account_ID>"
description = "this API-key is for my-robot"
}
Where:
service_account_id
: Service account ID. This is a required parameter.description
: Key description. This is an optional parameter.
Send your request:
export SERVICEACCOUNT_ID=<service_account_ID>
export IAM_TOKEN=<IAM_token>
curl \
--request POST \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $IAM_TOKEN" \
--data "{
\"serviceAccountId\": \"$SERVICEACCOUNT_ID\",
\"description\": \"this API-key is for my-robot\"
}" \
https://iam.api.cloud.yandex.net/iam/v1/apiKeys
Where:
SERVICEACCOUNT_ID
: Service account ID. This is a required parameter.IAM_TOKEN
: IAM token. This is a required parameter.
Deleting an API key
To delete a service account API key:
- In the management console
, navigate to the folder the service account belongs to. - From the list of services, select Identity and Access Management.
- In the left-hand panel, select
Service accounts and select the required service account. - Under API keys, click
in the row with the API key to delete, and select Delete. - In the window that opens, confirm the deletion.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
-
Get a list of API keys for a specific service account by entering the service account name:
yc iam api-key list \ --service-account-name <service_account_name>
Result:
+----------------------+---------------------+ | ID | CREATED AT | +----------------------+---------------------+ | ajenhvftf77r******** | 2022-03-13 21:15:40 | | ajeq610sgh05******** | 2022-03-13 21:14:43 | +----------------------+---------------------+
The folder specified in the CLI profile is used by default to search for the service account. You can specify a different folder through the
--folder-name
or--folder-id
parameter. -
Delete the API key by specifying its ID:
yc iam api-key delete <API_key_ID>
If you don't have Terraform, install it and configure the Yandex Cloud provider.
-
Open the Terraform configuration file and delete the section with the API key description.
Example API key description in the Terraform configuration:
resource "yandex_iam_service_account_api_key" "sa-api-key" { service_account_id = "<service_account_ID>" description = "<key_description>" pgp_key = "<PGP_key>" }
For more information about the resources you can create with Terraform, see the relevant provider documentation
. -
Make sure the configuration files are correct.
-
In the command line, go to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If you described the configuration correctly, the terminal will display a list of the resources being created and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy the cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
Confirm creating and deleting the resources by typing
yes
in the terminal and clicking Enter.
This will create or delete all resources you need in the specified folder. You can check the new resources and their settings or make sure the resources were deleted using the management console
and this CLI command:yc iam api-key list --service-account-id <service_account_ID>
-
Use the delete REST API method for the ApiKey resource:
export APIKEY_ID=<API_key_ID>
export IAM_TOKEN=<IAM_token>
curl \
--request DELETE \
--header "Authorization: Bearer $IAM_TOKEN" \
https://iam.api.cloud.yandex.net/iam/v1/apiKeys/$APIKEY_ID
You can also delete the API key using the ApiKeyService/Delete gRPC API call.