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

# How to manage static keys

[Static keys](https://docs.nebius.com/iam/authorization/static-keys.md) provide a method of authentication in Nebius AI Cloud services from third-party tools and systems.

## Prerequisites

> [tab] CLI
>
> 1. [Install and configure](https://docs.nebius.com/cli/install.md) the Nebius AI Cloud CLI.
>
> 2. To [list expiring keys](#listing-expiring-keys), install jq to filter and sort the JSON data returned by the Nebius AI Cloud CLI:
>
>    ```bash Ubuntu theme={null}
>    sudo apt-get install jq
>    ```
>
>    ```bash macOS theme={null}
>    brew install jq
>    ```

> [tab] Go SDK
>
> [Install and initialize the Nebius SDK for Go](https://docs.nebius.com/sdk/go/install-auth.md).

> [tab] Python SDK
>
> [Install and initialize the Nebius SDK for Python](https://docs.nebius.com/sdk/python/install-auth.md).

> [tab] JavaScript SDK
>
> [Install and initialize the Nebius SDK for JavaScript](https://docs.nebius.com/sdk/javascript/install-auth.md).

## Creating a static key

> [tab] CLI
>
> Run the following command:
>
> ```bash theme={null}
> nebius iam static-key issue \
>   --account-service-account-id=<service_account_ID> \
>   --service=<CONTAINER_REGISTRY|OBSERVABILITY> \
>   --expires-at=<date_in_ISO_8601_format> 
> ```
>
> In this command, change the following parameters:
>
> * `--account-service-account-id`: ID of the service account for which the static key is created.
>
> * `--service`: Service for which you create the static key. The following values are possible:
>
>   * `CONTAINER_REGISTRY`: [Container Registry](https://docs.nebius.com/container-registry/index.md)
>   * `OBSERVABILITY`: [Observability services](https://docs.nebius.com/observability/index.md)
>
> * `--expires-at` (optional): Date of expiration in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. The default lifetime of a static key is 6 months.
>
> Output example:
>
> ```yaml theme={null}
> ...
>     metadata:
>       parent_id: project-***
>     spec:
>       account:
>         service_account:
>           id: serviceaccount-***
>       service: CONTAINER_REGISTRY
>   resource_id: statickey-***
> token: ********
> ```
>
> > [!WARNING]
> >
> > Save the token from the `token` parameter of the output. You will not be able to access it later.
>
> You can also get the ID of the static key from the `resource_id` parameter. The ID can be helpful later if you need to get the information about the static key or revoke it.

> [tab] Go SDK
>
> Issue a static key, then get the resulting operation and read the token from the issue response:
>
> ```go theme={null}
> issueResponse, err := sdk.Services().IAM().V1().
>     StaticKey().Issue(
>         ctx,
>         &iam.IssueStaticKeyRequest{
>             Spec: &iam.StaticKeySpec{
>                 Account: &iam.Account{
>                     Type: &iam.Account_ServiceAccount_{
>                         ServiceAccount: &iam.Account_ServiceAccount{
>                             Id: "<service_account_ID>",
>                         },
>                     },
>                 },
>                 Service:   iam.StaticKeySpec_CONTAINER_REGISTRY,
>                 ExpiresAt: timestamppb.New(time.Now().AddDate(0, 1, 0)),
>             },
>         },
>     )
> if err != nil {
>     return err
> }
> staticKeyOperation, err := sdk.Services().IAM().V1().
>     StaticKey().GetOperation(
>         ctx,
>         &common.GetOperationRequest{
>             Id: issueResponse.GetOperation().GetId(),
>         },
>     )
> if err != nil {
>     return err
> }
> staticKeyToken := issueResponse.GetToken()
> ```
>
> In the code, set the following parameters:
>
> * `Id` in `Account_ServiceAccount`: ID of the service account for which the static key is created.
>
> * `Service`: Service for which you create the static key. The following values are possible:
>
>   * `iam.StaticKeySpec_CONTAINER_REGISTRY`: [Container Registry](https://docs.nebius.com/container-registry/index.md)
>   * `iam.StaticKeySpec_OBSERVABILITY`: [Observability services](https://docs.nebius.com/observability/index.md)
>
> * `ExpiresAt`: Expiration timestamp for the static key. This example sets it to one month from the current time.
>
> > [!WARNING]
> >
> > Save the token returned in `issueResponse.GetToken()`. You will not be able to access it later.
>
> You can also get the ID of the static key from the operation returned by `GetOperation`. The ID can be helpful later if you need to get the information about the static key or revoke it.

> [tab] Python SDK
>
> Issue a static key, then get the resulting operation and read the token from the issue response:
>
> ```python theme={null}
> static_key_service = StaticKeyServiceClient(sdk)
> issue_response = await static_key_service.issue(
>     IssueStaticKeyRequest(
>         spec=StaticKeySpec(
>             account=Account(
>                 service_account=Account__ServiceAccount(
>                     id="<service_account_ID>",
>                 ),
>             ),
>             service=StaticKeySpec__ClientService.CONTAINER_REGISTRY,
>             expiresAt=datetime.now(timezone.utc) + timedelta(days=30),
>         ),
>     ),
> )
> operation_service = static_key_service.operation_service()
> static_key_operation = await operation_service.get(
>     GetOperationRequest(id=issue_response.operation.id),
> )
> static_key_token = issue_response.token
> ```
>
> In the code, set the following parameters:
>
> * `id` in `Account__ServiceAccount`: ID of the service account for which the static key is created.
>
> * `service`: Service for which you create the static key. The following values are possible:
>
>   * `StaticKeySpec__ClientService.CONTAINER_REGISTRY`: [Container Registry](https://docs.nebius.com/container-registry/index.md)
>   * `StaticKeySpec__ClientService.OBSERVABILITY`: [Observability services](https://docs.nebius.com/observability/index.md)
>
> * `expiresAt`: Expiration timestamp for the static key. This example sets it to 30 days from the current time.
>
> > [!WARNING]
> >
> > Save the token returned in `issue_response.token`. You will not be able to access it later.
>
> You can also get the ID of the static key from the operation returned by `operation_service().get()`. The ID can be helpful later if you need to get the information about the static key or revoke it.

> [tab] JavaScript SDK
>
> Issue a static key, then get the resulting operation and read the token from the issue response:
>
> ```ts theme={null}
> const issueStaticKeyService = new StaticKeyService(sdk);
> const issueResponse = await issueStaticKeyService.issue(
>   IssueStaticKeyRequest.create({
>     metadata: ResourceMetadata.create({
>       parentId: "<project_ID>",
>     }),
>     spec: StaticKeySpec.create({
>       account: Account.create({
>         type: {$case: "serviceAccount", serviceAccount:
>           Account_ServiceAccount.create({id: "<service_account_ID>"})},
>       }),
>       service: StaticKeySpec_ClientService.CONTAINER_REGISTRY,
>       expiresAt: dayjs().add(1, 'month'),
>     }),
>   }),
> );
> const staticKeyOperation = await issueStaticKeyService
>   .getOperationService()
>   .get(
>     GetOperationRequest.create({id: issueResponse.operation.id}),
>   ).result;
> const staticKeyToken = issueResponse.token;
> ```
>
> In the code, set the following parameters:
>
> * `parentId` in `ResourceMetadata`: [Project ID](https://docs.nebius.com/iam/manage-projects.md#how-to-get-a-project-id-javascript-sdk) in which the static key is created.
>
> * `id` in `Account_ServiceAccount`: ID of the service account for which the static key is created.
>
> * `service`: Service for which you create the static key. The following values are possible:
>
>   * `StaticKeySpec_ClientService.CONTAINER_REGISTRY`: [Container Registry](https://docs.nebius.com/container-registry/index.md)
>   * `StaticKeySpec_ClientService.OBSERVABILITY`: [Observability services](https://docs.nebius.com/observability/index.md)
>
> * `expiresAt`: Expiration timestamp for the static key. This example sets it to one month from the current time.
>
> > [!WARNING]
> >
> > Save the token returned in `issueResponse.token`. You will not be able to access it later.
>
> You can also get the ID of the static key from the operation returned by `getOperationService().get()`. The ID can be helpful later if you need to get the information about the static key or revoke it.

## Listing static keys

> [tab] CLI
>
> To get all static keys in your tenant, run the following command:
>
> ```bash theme={null}
> nebius iam static-key list
> ```
>
> To get the details about a static key, run the following command:
>
> ```bash theme={null}
> nebius iam static-key get --id=<static_key_ID>
> ```

> [tab] Go SDK
>
> To get all static keys in your tenant:
>
> ```go theme={null}
> staticKeyList, err := sdk.Services().IAM().V1().StaticKey().
>     List(
>     ctx,
>     &iam.ListStaticKeysRequest{},
> )
> if err != nil {
>     return err
> }
> fmt.Println(staticKeyList)
> ```
>
> To get the details about a static key:
>
> ```go theme={null}
> retrievedStaticKey, err := sdk.Services().IAM().V1().
>     StaticKey().Get(
>     ctx,
>     &iam.GetStaticKeyRequest{Id: "<static_key_ID>"},
> )
> if err != nil {
>     return err
> }
> fmt.Println(retrievedStaticKey)
> ```
>
> In the code, set `Id` to the ID of the static key.

> [tab] Python SDK
>
> To get all static keys in your tenant:
>
> ```python theme={null}
> static_key_service = StaticKeyServiceClient(sdk)
> static_keys = await static_key_service.list(
>     ListStaticKeysRequest(),
> )
> print(static_keys)
> ```
>
> To get the details about a static key:
>
> ```python theme={null}
> static_key_service = StaticKeyServiceClient(sdk)
> static_key = await static_key_service.get(
>     GetStaticKeyRequest(id="<static_key_ID>"),
> )
> print(static_key)
> ```
>
> In the code, set `id` to the ID of the static key.

> [tab] JavaScript SDK
>
> To get all static keys in your tenant:
>
> ```ts theme={null}
> const listStaticKeyService = new StaticKeyService(sdk);
> const staticKeys = await listStaticKeyService.list(
>   ListStaticKeysRequest.create({}),
> );
> console.log(staticKeys);
> ```
>
> To get the details about a static key:
>
> ```ts theme={null}
> const getStaticKeyService = new StaticKeyService(sdk);
> const staticKey = await getStaticKeyService.get(
>   GetStaticKeyRequest.create({id: "<static_key_ID>"}),
> );
> console.log(staticKey);
> ```
>
> In the code, set `id` to the ID of the static key.

## Listing expiring keys

After a static key expires, you can't use it for authentication. To find the keys that you need to replace soon, list the keys that expire within the next seven days, across all service accounts in your project.

> [tab] CLI
>
> List the expiring keys, sorted by expiration date:
>
> ```bash theme={null}
> nebius iam static-key list --all --format json \
>   | jq '(now | todate) as $now
>       | (now + 7 * 86400 | todate) as $cutoff
>       | [.items[]?
>           | select(.spec.expiresAt != null
>               and .spec.expiresAt >= $now
>               and .spec.expiresAt <= $cutoff)]
>       | sort_by(.spec.expiresAt)
>       | .[]
>       | {
>           id: .metadata.id,
>           service_account_id: .spec.account.service_account.id,
>           expires_at: .spec.expiresAt
>         }'
> ```
>
> For each key, the output contains the following fields:
>
> * `id`: ID of the key.
> * `service_account_id`: ID of the service account that the key belongs to.
> * `expires_at`: Date and time when the key expires.
>
> To adjust the command:
>
> * To change the period, replace `7` in `7 * 86400` with another number of days.
> * To list keys in a project other than the one saved in your CLI profile, add `--parent-id <project_ID>` to the command.

> [tab] Go SDK
>
> List the expiring keys, sorted by expiration date:
>
> ```go theme={null}
> staticKeyListService := sdk.Services().IAM().
>     V1().StaticKey()
> now := time.Now()
> cutoff := now.Add(7 * 24 * time.Hour)
> var expiringStaticKeys []*iam.StaticKey
> staticKeyPageToken := ""
> for {
>     staticKeys, err := staticKeyListService.List(
>         ctx,
>         &iam.ListStaticKeysRequest{
>             ParentId:  "<project_ID>",
>             PageSize:  1000,
>             PageToken: staticKeyPageToken,
>         },
>     )
>     if err != nil {
>         return fmt.Errorf("list static keys: %w", err)
>     }
>     for _, key := range staticKeys.GetItems() {
>         if key.GetSpec().GetExpiresAt() == nil {
>             continue
>         }
>         expiresAt := key.GetSpec().GetExpiresAt().AsTime()
>         if expiresAt.Before(now) || expiresAt.After(cutoff) {
>             continue
>         }
>         expiringStaticKeys = append(expiringStaticKeys, key)
>     }
>     staticKeyPageToken = staticKeys.GetNextPageToken()
>     if staticKeyPageToken == "" {
>         break
>     }
> }
> sort.Slice(expiringStaticKeys, func(i, j int) bool {
>     first := expiringStaticKeys[i].GetSpec().GetExpiresAt()
>     second := expiringStaticKeys[j].GetSpec().GetExpiresAt()
>     return first.AsTime().Before(second.AsTime())
> })
> for _, key := range expiringStaticKeys {
>     fmt.Println(
>         key.GetMetadata().GetId(),
>         key.GetSpec().GetAccount().GetServiceAccount().GetId(),
>         key.GetSpec().GetExpiresAt().AsTime().
>             Format(time.RFC3339),
>     )
> }
> ```
>
> In the code, set `ParentId` to the [project ID](https://docs.nebius.com/iam/manage-projects.md#how-to-get-a-project-id-go-sdk).
>
> For each key, the code prints the following values:
>
> * ID of the key.
> * ID of the service account that the key belongs to.
> * Date and time when the key expires.
>
> To change the period, replace `7` in `7 * 24 * time.Hour` with another number of days.

> [tab] Python SDK
>
> List the expiring keys, sorted by expiration date:
>
> ```python theme={null}
> static_key_list_service = StaticKeyServiceClient(sdk)
> now = datetime.now(timezone.utc)
> cutoff = now + timedelta(days=7)
> expiring_static_keys = []
> static_key_page_token = ""
> while True:
>     static_keys = await static_key_list_service.list(
>         ListStaticKeysRequest(
>             parent_id="<project_ID>",
>             page_size=1000,
>             page_token=static_key_page_token,
>         ),
>     )
>     for key in static_keys.items:
>         expires_at = key.spec.expiresAt
>         if expires_at and now <= expires_at <= cutoff:
>             expiring_static_keys.append(key)
>     static_key_page_token = static_keys.next_page_token
>     if not static_key_page_token:
>         break
> expiring_static_keys.sort(key=lambda key: key.spec.expiresAt)
> for key in expiring_static_keys:
>     service_account = key.spec.account.service_account
>     print(
>         key.metadata.id,
>         service_account.id if service_account else None,
>         key.spec.expiresAt.isoformat(),
>     )
> ```
>
> In the code, set `parent_id` to the [project ID](https://docs.nebius.com/iam/manage-projects.md#how-to-get-a-project-id-python-sdk).
>
> For each key, the code prints the following values:
>
> * ID of the key.
> * ID of the service account that the key belongs to.
> * Date and time when the key expires.
>
> To change the period, replace `7` in `timedelta(days=7)` with another number of days.

> [tab] JavaScript SDK
>
> List the expiring keys, sorted by expiration date:
>
> ```ts theme={null}
> const staticKeyListService = new StaticKeyService(sdk);
> const now = dayjs();
> const cutoff = now.add(7, "day");
> const expiringStaticKeys = [];
> let staticKeyPageToken = "";
> while (true) {
>   const staticKeys = await staticKeyListService.list(
>     ListStaticKeysRequest.create({
>       parentId: "<project_ID>",
>       pageSize: 1000,
>       pageToken: staticKeyPageToken,
>     }),
>   ).result;
>   for (const key of staticKeys.items) {
>     const expiresAt = key.spec?.expiresAt;
>     if (
>       expiresAt &&
>       !expiresAt.isBefore(now) &&
>       !expiresAt.isAfter(cutoff)
>     ) {
>       expiringStaticKeys.push(key);
>     }
>   }
>   if (!staticKeys.nextPageToken) {
>     break;
>   }
>   staticKeyPageToken = staticKeys.nextPageToken;
> }
> expiringStaticKeys.sort(
>   (first, second) =>
>     first.spec.expiresAt.valueOf() -
>     second.spec.expiresAt.valueOf(),
> );
> for (const key of expiringStaticKeys) {
>   const account = key.spec.account?.type;
>   console.log({
>     id: key.metadata?.id,
>     serviceAccountId:
>       account?.$case === "serviceAccount"
>         ? account.serviceAccount.id
>         : undefined,
>     expiresAt: key.spec.expiresAt.toISOString(),
>   });
> }
> ```
>
> In the code, set `parentId` to the [project ID](https://docs.nebius.com/iam/manage-projects.md#how-to-get-a-project-id-javascript-sdk).
>
> For each key, the code prints an object with the following properties:
>
> * `id`: ID of the key.
> * `serviceAccountId`: ID of the service account that the key belongs to.
> * `expiresAt`: Date and time when the key expires.
>
> To change the period, replace `7` in `now.add(7, "day")` with another number of days.

The list doesn't include keys that have already expired or keys without an expiration date. If the list is empty, no keys expire within the next seven days.

To replace an expiring key, [create a new static key](#creating-a-static-key).

## Revoking a static key

> [tab] CLI
>
> You can revoke a static key by using the following command:
>
> ```bash theme={null}
> nebius iam static-key delete --id=<static_key_ID>
> ```
>
> If you only know the token value, run the following command to revoke the token:
>
> ```bash theme={null}
> nebius iam static-key revoke --token=<static_key_token> 
> ```

> [tab] Go SDK
>
> You can revoke a static key by using the following code:
>
> ```go theme={null}
> deleteStaticKey, err := sdk.Services().IAM().V1().
>     StaticKey().Delete(
>         ctx,
>         &iam.DeleteStaticKeyRequest{
>             Id: "<static_key_ID>",
>         },
>     )
> if err != nil {
>     return err
> }
> if _, err = deleteStaticKey.Wait(ctx); err != nil {
>     return err
> }
> ```
>
> In the code, set `Id` to the ID of the static key.
>
> If you only know the token value, use the following code to revoke the token:
>
> ```go theme={null}
> revokeStaticKey, err := sdk.Services().IAM().V1().
>     StaticKey().Revoke(
>         ctx,
>         &iam.RevokeStaticKeyRequest{
>             Token: "<static_key_token>",
>         },
>     )
> if err != nil {
>     return err
> }
> if _, err = revokeStaticKey.Wait(ctx); err != nil {
>     return err
> }
> ```
>
> In the code, set `Token` to the static key's token value.

> [tab] Python SDK
>
> You can revoke a static key by using the following code:
>
> ```python theme={null}
> static_key_service = StaticKeyServiceClient(sdk)
> delete_static_key = await static_key_service.delete(
>     DeleteStaticKeyRequest(id="<static_key_ID>"),
> )
> await delete_static_key.wait()
> ```
>
> In the code, set `id` to the ID of the static key.
>
> If you only know the token value, use the following code to revoke the token:
>
> ```python theme={null}
> static_key_service = StaticKeyServiceClient(sdk)
> revoke_static_key = await static_key_service.revoke(
>     RevokeStaticKeyRequest(token="<static_key_token>"),
> )
> await revoke_static_key.wait()
> ```
>
> In the code, set `token` to the static key's token value.

> [tab] JavaScript SDK
>
> You can revoke a static key by using the following code:
>
> ```ts theme={null}
> const deleteStaticKeyService = new StaticKeyService(sdk);
> const deleteStaticKey = await deleteStaticKeyService.delete(
>   DeleteStaticKeyRequest.create({id: "<static_key_ID>"}),
> ).result;
> await deleteStaticKey.wait();
> ```
>
> In the code, set `id` to the ID of the static key.
>
> If you only know the token value, use the following code to revoke the token:
>
> ```ts theme={null}
> const revokeStaticKeyService = new StaticKeyService(sdk);
> const revokeStaticKey = await revokeStaticKeyService.revoke(
>   RevokeStaticKeyRequest.create({token: "<static_key_token>"}),
> ).result;
> await revokeStaticKey.wait();
> ```
>
> In the code, set `token` to the static key's token value.
