# Create and manage API keys

> How to create a CNPJá API key and use it in your requests, who manages keys, and how to rename them, revoke a leaked key and view each key's usage.

API keys authenticate your application's requests to the API on behalf of the organization. Owners, Administrators and Developers create, rename and revoke keys under **Settings** > **API keys**, at the foot of the rail (on a phone, tap **Menu** > **Settings**).

## The key belongs to the organization

Each key belongs to the [organization](https://cnpja.com/en-us/docs/organizations/manage#what-it-is), not to the person who created it:

- *Same access:* every key runs the same operations. There is no key restricted to some operations.
- *Same quota:* the usage of every key comes out of the same [quota](https://cnpja.com/en-us/docs/billing/queries-and-quotas) and the same overage budget, and their requests count toward the same [request limits](https://cnpja.com/en-us/docs/api/limits-and-errors).
- *Same data:* a list created with one key can be used with any other.
- *No expiry:* a key stays valid until it is revoked, even when whoever created it leaves the organization, as [Keys of members who leave](https://cnpja.com/en-us/docs/organizations/members-and-roles#keys-of-leavers) explains.

More keys never raise the quota or the limits. The benefit is keeping applications apart: with one key each, you track each application's usage and revoke one key without stopping the others.

> **Important:** treat the key like a password. Whoever holds it looks up data and consumes the organization's quota. Keep it on the server only, never in source code, web pages or mobile apps. What the organization takes on for the use of its keys is set out in the [Terms of Use](https://cnpja.com/en-us/legal/terms).

## Who can manage

Owner, Administrator and Developer. Bookkeepers and Members do not see **API keys** under **Settings**.

To manage keys, ask an Owner or Administrator for the Developer role, the least privileged role that manages keys, which manages neither members nor the subscription. The full matrix is in [Permissions by role](https://cnpja.com/en-us/docs/organizations/members-and-roles#matrix).

## Create a key

1. Open **Settings** > **API keys**.
2. Click **Create key** at the top of the page.
3. Under **Name**, enter the name of the application that will use the key, such as "Production server".
4. Under **Color**, pick one of the nine colors; a new key starts as **Blue**. The color identifies the key in the table and in the **Usage** charts, where a **Teal** or **Gray** key is drawn in another color, since in the charts those are the colors of live and cached queries.
5. Click **Create key**.
6. In the **Key created** window, click the **Copy key** icon next to the key and store the key straight in its final place, such as an environment variable or a secrets vault.
7. Click **Done**.

> **Important:** the full key is shown only once. After that, the page shows only its last characters. If you try to close the window without copying, the app asks **Close without copying the key?**. A lost key cannot be recovered: create another and revoke the lost one.

Name each key after the application or environment that uses it, such as production and staging. The page does not show who created each key, and the name is what identifies the key under **Usage** and **Requests**. Owners and Administrators can filter the [audit log](https://cnpja.com/en-us/docs/organizations/audit-log) by **Actor** to see the API keys a member created.

## Rename and change the color

The name and color of an active key change on its own row, in the **Active** tab:

- *Name:* click the name, edit it and press `Enter` to save or `Esc` to undo. Leaving the field also saves.
- *Color:* click the colored dot at the start of the row, the **Change color** button, and pick another color.

The app confirms each change with a notice. The change does not affect the key or its requests, and a revoked key can no longer be changed: if someone else revoked the key after the page opened, the change, or the revocation, is refused with **This key was already revoked and can no longer be changed.**, and the key moves to the **Revoked** tab.

## Revoke

Revoke a key when it leaks, for example in a repository, a log or a message, and when the application stops using it.

1. In the **Active** tab, click the **Revoke key** icon at the end of the key's row. With a mouse, the row's icons appear when you hover over it.
2. In the confirmation, check the key's name and ending.
3. Click **Revoke key**.

> **Important:** revoking applies right away and cannot be undone. Requests with the key are refused with `401` from then on, including those of applications in production.

The revoked key moves to the **Revoked** tab, with its date under **Revoked**, and frees a slot in the active key limit, but still counts toward the organization's total of keys. Its usage and requests remain under **Usage** and **Requests**.

To replace a key without stopping your application, create the new one, update the application and only then revoke the old one, as [Authentication](https://cnpja.com/en-us/docs/api/authentication) explains. If the key leaked, revoke it first.

## View a key's usage

1. In the **Active** tab, click the **View this key's usage** icon at the end of the key's row.
2. The **Usage** page opens with the **Actor** filter already set to that key.

Under **Requests**, the same **Actor** filter shows every request made with the key. The filters are covered in [Filter by member or key](https://cnpja.com/en-us/docs/billing/usage#actor).

## Limits

The organization has a limit of active keys and another of keys in total, revoked ones included. The **Active keys** counter, at the top of the page, shows how many are in use; at the active limit, **Create key** is disabled, with a notice naming the limit reached, until you revoke one. At the total limit, **Create key** is disabled too, with another notice, and revoking a key frees no slot. The current limits are in [Usage limits](https://cnpja.com/en-us/docs/billing/limits#organization).

The **Getting started** panel shows the **API limit** of the organization's plan, in **Requests per second** and **Concurrent requests**, which all keys share. In a Free organization, whoever may subscribe also sees **A higher limit on Pro**, which opens [checkout](https://cnpja.com/en-us/docs/billing/subscribe). The value for each plan and when it changes are in [Limits and errors](https://cnpja.com/en-us/docs/api/limits-and-errors#with-key).

## Use the key in the API

Send the full key in the `Authorization` header of every request, with no `Bearer` prefix. The **Getting started** panel, at the bottom of the **API keys** page, has a curl example. The walkthrough is in [First request](https://cnpja.com/en-us/docs/api/first-request), and sending, rotating and `401` errors are in [Authentication](https://cnpja.com/en-us/docs/api/authentication).
