# Create an API key

> Give your own code access to Soosh, with only the permissions it needs, and replace or revoke the key later.

Source: https://docs.soosh.io/guides/integrations/create-an-api-key/

## Before you start

- Owner or admin role in Soosh
- A plan that includes API keys

## Create the key

1. Go to [**Settings > API keys**](https://app.soosh.io/settings/api-keys) and click **Create API key**.
2. In **Name**, enter where it will be used, like *Order system*.
3. Optional: in **Expires**, choose the last day it works.
4. Optional: under **Allowed IP addresses**, enter the addresses your code
   calls from, one per line, like `203.0.113.7` or `203.0.113.0/24`. Up to
   20. Leave it empty to allow any address.
5. Under **What this key can do**, tick only what your code needs. A choice
   marked **Not in your plan** or **You don't have this permission** can't be
   ticked.

   *Picture: What this key can do, with Send messages, upload media, Read messages and media, and List and read contacts ticked*
6. Click **Create key**.
7. Click **Copy key** (or tick **I've copied the key**), keep the key somewhere
   safe, and click **Done**.

> **Careful:** Soosh shows the key only once. Anyone with it can use your workspace within
> what you ticked. If it leaks, open the key in [**Settings > API keys**](https://app.soosh.io/settings/api-keys)
> and click **Revoke**.

## Result

The key starts with `soosh_live_`. Send it in the `Authorization` header,
as `Bearer` and the key, with every request to `https://app.soosh.io/v1/`.
See [Authentication](https://docs.soosh.io/reference/api/#authentication) in the API reference, or
[Make your first API call](https://docs.soosh.io/get-started/make-your-first-api-call/).

A request for something the key can't do is refused with `403`
[`scope_missing`](https://docs.soosh.io/reference/api/errors/#scope_missing), and `error.details.scope`
names the permission it needed. A request from an address you didn't allow is
[`ip_not_allowed`](https://docs.soosh.io/reference/api/errors/#ip_not_allowed).

The key belongs to the workspace, but it follows your access: it can never do
more than you can. See [When a key stops working on its own](#when-a-key-stops-working-on-its-own).
Keys with **List and
read contacts** or **Read broadcasts and recipients** see full phone numbers,
even when your workspace masks them for members. The list shows when
each key was last used, and from which address. You and the workspace owner
get an email when a key is created, and again 7 days before it expires.

## Use a key with several numbers

The permissions you choose apply to all WhatsApp numbers in the workspace.
Creating a key called *Support* does not restrict it to your support number.
Keep the permissions to what that system needs, and choose the sending
number in each request. See
[Use several numbers with the API](https://docs.soosh.io/guides/api/use-several-numbers/).

## Replace a key

Rotate a key to get a new one while the old one keeps working for a while,
so you can switch over without stopping.

1. Go to [**Settings > API keys**](https://app.soosh.io/settings/api-keys) and click the key.
2. Click **Rotate**.
3. Under **Old key keeps working for**, choose **Now**, **1 hour**,
   **24 hours** or **7 days**.
4. Click **Rotate key**.
5. Click **Copy key** (or tick **I've copied the key**), put the new key in your
   code, and click **Done**.

The new key has the same name, permissions, allowed addresses and expiry.
You can only rotate a key whose permissions you have yourself. Broadcasts the
old key started, and webhooks it made, carry on with the new one. A request
retried with the new key under the same `Idempotency-Key` gets the answer the
old key was given.

A key that has expired, or was already rotated, can't be rotated. Nor can
one whose previous version still works: wait until the old key stops, or
revoke it, first.

## Revoke a key

Open the key, click **Revoke**, then **Revoke key**. Anything using it stops
working at once, and this can't be undone. Broadcasts it started pause, and
webhooks it made are turned off. Once a key is revoked or expired, you can
click **Delete** to take it off the list.

If your plan stops including API keys, your keys stop working, but you can
still open, revoke and delete them.

## When a key stops working on its own

A key follows the access of the person who made it. Soosh revokes it, at
once and for good, when that person:

- is removed from the workspace: every key they made in it
- loses a permission the key uses, because they get a new role, their role is
  changed, or ownership moves on and they become an Admin: only the keys that
  use it
- is set to work on only some WhatsApp numbers: the keys that read or send
  chats, contacts, templates, campaigns or numbers, since a key works on
  every number

Only what a change takes away counts. A change that gives them more, or
takes away something the key doesn't use, leaves the key working. Their keys
in other workspaces aren't touched by a change in this one.

Before you remove someone, change their role or their numbers, edit a role,
or transfer ownership, Soosh says how many keys will stop working, like
*Their 2 API keys will stop working.* Each key that stops shows in the
[audit log](https://docs.soosh.io/guides/workspace/read-the-audit-log/) with why, under the
person who made the change. A rotated key counts as made by whoever rotated
it.

For a key that must outlive any one person, have someone who will stay, like
the owner, make it.

### Keys made by someone who left

Keys made before this rule, by someone who has since left, still work. The
list marks them **Made by someone who left**. If your code still uses one,
make a new key, put it in your code, then revoke the old one.

## Related

- [Make your first API call](https://docs.soosh.io/get-started/make-your-first-api-call/)
- [Handle errors and limits](https://docs.soosh.io/guides/api/errors-and-limits/)
- [Introduction](https://docs.soosh.io/reference/api/)
- [Errors](https://docs.soosh.io/reference/api/errors/)
- [Permissions](https://docs.soosh.io/reference/permissions/)
