API key approval
An API key belongs to you and one tenant. It acts with your current role, limited to the scopes you approve. Cost data and virtual tags stay limited to that tenant. Theorganization:manage scope permits supported organization management operations across the organization. It does not expand access to tenant data.
If your role changes, the key’s access changes with it. See Roles and permissions for how roles and scopes combine.
Browser approval and key storage
The browser approval names the tenant and every requested scope. After approval, the CLI prints the key once and revokes the temporary approval. Creating a key does not sign you in or change your saved session. The CLI does not save the new key. Store it in your secret manager.Requirements
You need the following:- The CostGraph CLI 0.11.0 or later. Refer to Install and sign in.
- A browser on the same machine as the CLI, for the approval page.
- Access to the tenant that holds the key.
- The Owner role, if the key includes
organization:manage. Keys withpricing:writeordeployment:registerneed the Admin role or higher.
costgraph auth login first. The approval page asks you to sign in if your browser has no CostGraph session.
Choose a tenant
Every key belongs to one tenant. Its access to cost data, virtual tags, and other tenant data stays within that tenant. Choose the tenant that holds the data your workflow uses. List your tenants with their names and IDs.costgraph api-keys create with --tenant. The flag accepts a tenant name or ID, and names match without regard to case. Without --tenant, you choose the tenant on the approval page.
If you pass --tenant and then select a different tenant on the approval page, the CLI stops with browser approval selected a different tenant; no key was created.
The key creation output reports the tenant ID. In Terraform, select the tenant by name with the costgraph_tenant data source and use its returned .id. Refer to Use a tenant in Terraform.
Manage scopes
A scope is one permission that you attach to a key. Give a key only the scopes its workflow needs.List supported scopes
The following command lists the scopes that your CostGraph deployment supports.COSTGRAPH_API_URL first for a self-hosted deployment.
Add -o json to return a JSON array.
Choose scopes for a task
The following table lists the scopes for some common workflows.virtual-tags:write includes read access. These scopes apply to the virtual tag management API used by Terraform.
The CLI’s costgraph tags commands require a browser session. See Credentials.
organization:manage permits owner-level organization setup, such as creating tenants, across the key’s organization. It doesn’t grant access to virtual tags or other data in a tenant. A key that manages the organization and also writes virtual tags needs both scopes. Only an organization owner can grant organization:manage.
Refer to Roles and permissions for what other scopes allow.
Attach several scopes to one key
Repeat--scope once for each permission. Every scope you pass goes on the same key. The CLI doesn’t create one key per scope.
--scope also accepts a comma-separated list, such as --scope focus:read,virtual-tags:write. Both forms produce one key with the same scopes. The CLI removes duplicates and rejects a value that contains spaces.
API key scopes and virtual tag rule scopes
The two--scope flags have different meanings.
Create an API key
This procedure creates a key namedteam-tags with virtual-tags:write in the Production tenant. Replace the name, tenant, and scopes with the ones your workflow needs.
Request the key
--name is required, and you must pass at least one --scope. The CLI opens the approval page in your browser and prints the same address. If the browser doesn’t open, copy the address into a browser on the same machine.Approve the tenant and scopes
Copy the key
--callback-port to use another port.
Capture the key in a script
With-o json, the CLI prints the new key as JSON on standard output. Approval instructions still go to standard error, so they stay visible in your terminal. The JSON has the fields id, name, tenant_id, permission_scopes, and raw_api_key.
The following commands store the key in an environment variable without printing it. They export the variable only when the CLI returns a key.
jq -e rejects a missing or null raw_api_key. If capture fails, the else branch leaves COSTGRAPH_API_KEY unchanged.
The variable lasts only for the current shell. Save the key in a secret store if you need it later.
Use the key in a script or CI
The CLI reads an API key from theCOSTGRAPH_API_KEY environment variable. Store the key as a secret in your CI system and expose it to the job under that name.
The following job step prints month-to-date cost by provider. It needs a key with focus:read.
COSTGRAPH_API_URL to your API address.
Create the key on a workstation before the job runs. Key creation always needs an approval in a browser, and -o json doesn’t change that. A CI job can only use a key that already exists.
The CLI prefers a saved session over an API key. A CI runner normally has no saved session. On a workstation where you signed in, run costgraph auth logout before you test commands with the key. Refer to Credentials.
Create a key for Terraform
A configuration that manages tenants and virtual tags needs two permissions on one key. Only an organization owner can grant the management permission.tf-eval by name with the costgraph_tenant data source. Pass its returned .id to virtual tag resources so they use the key’s tenant.
The provider reads the key from COSTGRAPH_API_KEY. Follow Capture the key in a script to set it without printing the secret.
Retry after a partial Terraform apply
If Terraform creates some resources before a virtual tag fails, correct the key’s tenant, scopes, or owner role. The virtual tag key needsvirtual-tags:write for that tenant. organization:manage does not grant this access.
Keep the successfully created resources in Terraform state. Run terraform plan again after correcting access, review the result, and apply the new plan.
Manage existing keys
The CLI creates keys and lists supported scopes. It doesn’t list, update, rotate, or revoke existing keys. Manage your keys in the CostGraph dashboard under Settings > Account Settings > API Keys.Resolve permission errors
CostGraph refuses a request with HTTP401 or 403 for several reasons. A missing scope is only one of them. The CLI prints the reason after Error:, followed by the status code. Check each of the following in order.
The command doesn't accept API keys
The command doesn't accept API keys
Valid Bearer token required (401) means the command accepts only a saved session. Refer to Credentials for the commands that reject API keys. Run the command after costgraph auth login instead.The CLI used your saved session
The CLI used your saved session
COSTGRAPH_API_KEY. Run costgraph auth status. If it reports browser, the command ran as your session. Run costgraph auth logout, then run the command again with the key.costgraph auth status reports api_key when it finds a key and no saved session. It doesn’t check the key’s scopes or whether the key still works.The key belongs to another tenant
The key belongs to another tenant
organization:manage covers supported organization management operations across tenants.Compare the tenant ID in the key creation output with your command or configuration. Create a key in the correct tenant if they differ.The key lacks a scope
The key lacks a scope
Insufficient permissions message names the missing scope. Check Choose scopes for a task. organization:manage does not grant virtual tag access.To add permissions, create a replacement key with the full set of scopes the workflow needs.Your role doesn't allow the action
Your role doesn't allow the action
The CLI is pointed at another deployment
The CLI is pointed at another deployment
costgraph config list and check the API address and where it came from. Set COSTGRAPH_API_URL to the deployment that issued the key. A saved session also belongs to the deployment where you signed in, so sign in again after you change the address.The key was revoked or another credential was used
The key was revoked or another credential was used
API key is inactive. Check the key under Settings > Account Settings > API Keys. Installation tokens, such as the token an operator or agent uses, can’t create API keys.Key creation errors
Choose the error category reported by the CLI.Missing name or scopes
Missing name or scopes
--name is required means you must name the key.at least one --scope is required means you must request a permission. Run costgraph api-keys scopes for supported names.each --scope must name one permission means a value contains spaces. Repeat --scope or separate values with commas.Tenant or scope approval mismatch
Tenant or scope approval mismatch
--tenant.If the CLI asks you to approve the scopes for the tenant, run it again and approve every requested scope.Deployment does not support key creation
Deployment does not support key creation
COSTGRAPH_API_URL. Use the dashboard to create the key on that deployment.Your role cannot grant a permission
Your role cannot grant a permission
organization:manage. Remove that scope or ask an owner to create the key.Only admins can create keys that set prices or register deployments. Remove pricing:write and deployment:register, or ask an admin.A key with this name already exists
A key with this name already exists
--name.The temporary approval was not revoked
The temporary approval was not revoked
Could not revoke the temporary approval session means CostGraph created the key but left the approval session active.Remove it under Settings > Account Settings > Connected apps.Command reference
Commands are shown without thecostgraph prefix. Add --help for the complete flag list.
API key commands
Run each command with thecostgraph prefix.
raw_api_key once. Does not change the saved CLI login.Key creation flags
Pass these flags tocostgraph api-keys create.