# Authenticate with an API key

> Create an API key, limit it to some sites, choose whether it can make changes, and revoke it when you're done.

Every request needs an API key. A key acts for the person who made it, on one team, so it reads what that person can read in Onsite.

## Before you start

- A team with an active Onsite plan.
- Any role on the team can make keys. A key can never do more than its maker's role allows, so a viewer's key reads but can't change anything.

## Create a key

<Steps>
  <Step title="Open API keys">
    In Onsite, go to [**Settings › API keys**](https://onsite.so/settings/api-keys) and select **Create key**.
  </Step>
  <Step title="Name it">
    Name it after where it will run, such as "Looker Studio" or "Weekly report script", so you know which one to revoke later.
  </Step>
  <Step title="Choose its sites">
    Leave **All sites** on to read every site you can, including sites added later. Or pick the sites it may read: it can't see any other site, and it can't read portfolios.
  </Step>
  <Step title="Choose whether it can make changes">
    Keys only read unless you turn on **Can make changes**. Turn it on for a key that tracks keywords, updates alerts, adds notes or starts exports.
  </Step>
  <Step title="Copy the key">
    Onsite shows the key once. Copy it and store it somewhere safe, like your password manager or your platform's secret settings. If you lose it, revoke it and create another.
  </Step>
</Steps>

Keys start with `oapi_` and last one year. Settings › API keys shows when each one expires and when it was last used.

## Send the key

Put the key in the `Authorization` header of every request, after `Bearer`:

<CodeGroup>
```bash title="curl"
curl "https://api.onsite.so/api/v1/sites" \
  -H "Authorization: Bearer $ONSITE_API_KEY"
```

```javascript title="JavaScript"
const response = await fetch("https://api.onsite.so/api/v1/sites", {
  headers: { Authorization: `Bearer ${process.env.ONSITE_API_KEY}` },
});
const { sites } = await response.json();
```

```python title="Python"
import os
import requests

response = requests.get(
    "https://api.onsite.so/api/v1/sites",
    headers={"Authorization": f"Bearer {os.environ['ONSITE_API_KEY']}"},
)
sites = response.json()["sites"]
```
</CodeGroup>

<Warning>
Treat a key like a password. Don't put it in a web page, a mobile app or a public repository, where anyone can read it. Call the API from a server or a script instead.
</Warning>

## What a key can do

| Key | Reads | Makes changes |
|---|---|---|
| All sites | Every site its maker can read, and portfolios | Only with **Can make changes** |
| Some sites | Only the sites picked when it was made | Only with **Can make changes**, on those sites |

A key also follows its maker's role. If their role can't do something in Onsite, the key can't do it either.

## Revoke a key

In [**Settings › API keys**](https://onsite.so/settings/api-keys), select **Revoke** next to the key. It stops working at once: the next request with it gets a `401`.

A key also stops working when it expires, and when the person who made it leaves the team.

## If something goes wrong

<AccordionGroup>
  <Accordion title="401 unauthenticated">
    The key is missing, mistyped, revoked or expired. Check that the header reads `Authorization: Bearer oapi_...` with no quotes or spaces inside the key. If the key was revoked or expired, create a new one.
  </Accordion>
  <Accordion title="403 This key can only read">
    The request makes a change, and the key doesn't have **Can make changes**. Create a key with it turned on. A key's settings can't be changed after it's made.
  </Accordion>
  <Accordion title="403 This key is limited to some sites">
    Portfolios read every site on the team, so they need a key for all sites.
  </Accordion>
  <Accordion title="404 This key can't read a site">
    The site isn't one of the key's sites, or the name doesn't match one. `error.didYouMean` suggests the closest site the key can read.
  </Accordion>
  <Accordion title="402 payment_required">
    The team's plan has ended. An owner can renew it in **Settings › Billing**.
  </Accordion>
</AccordionGroup>

## Related

<CardGroup cols={2}>
  <Card title="Errors" icon="circle-alert" href="/api/errors">
    Every error code and what to do about it.
  </Card>
  <Card title="Rate limits" icon="gauge" href="/api/rate-limits">
    How many requests a key can make.
  </Card>
</CardGroup>
