---
title: Keys and tokens
description: The credentials Luna issues, what each one reaches and which one is safe to ship in an app.
related:
  - /concepts/organisations-projects-and-environments
  - /lunas/define-a-luna
---

A key or token is what a caller shows Luna to be let in. Luna issues four kinds, and each reaches a different part of it.

| Credential | Starts with | Reaches | Safe in an app |
| --- | --- | --- | --- |
| Publishable key | `pk_` | The public luna types of one environment | Yes |
| Secret token | `sk_` | Every luna type of one environment | No |
| API token | `lt_` | The control API, within the scopes it was given | No |
| Account key | `la_` | Everything your account may do | No |

```mermaid
flowchart LR
  App[Your app] -- publishable key --> Pub[Public luna types]
  Server[Your server] -- secret token --> All[Every luna type]
  CI[CI or a platform] -- API token --> API[Control API]
  CLI[The luna CLI] -- account key --> API
```

## Publishable keys

A publishable key lets an app call an environment's public luna types. It is safe to ship in a web page or a mobile app.

Once an environment has been deployed to, `luna env use`, `luna dev` and `luna deploy` write its URL and a publishable key to the folder's `.env.local`, as `LUNA_URL` and `LUNA_KEY`. A call sends the key in the `luna-key` header.

A luna type is private unless it says `visibility: "public"`. A publishable key cannot reach a private type, and the type answers as if it did not exist.

You can narrow a publishable key further.

- `--origin` lets it work only from the web origins you name.
- `--type` lets it call only the luna types you name.
- `--rate` caps its calls each second. The default is 100 a second.
- `--expires-in` ends it after a time.

## Secret tokens

A secret token reaches every luna type of an environment, private ones included. Use it from your own server, never from an app.

A call sends it as `Authorization: Bearer <TOKEN>`. Luna shows a secret token once, when you create it.

## Keys of an environment

Every environment starts with one publishable key and one secret token. `luna keys` manages them and any you add.

| Command | Does |
| --- | --- |
| `luna keys list` | Lists the environment's keys and tokens |
| `luna keys create` | Creates a publishable key, or a secret token with `--secret` |
| `luna keys rotate <ID>` | Replaces a key, and the old one keeps working for an hour, or as long as `--overlap` says |
| `luna keys revoke <ID>` | Ends a key at once |

## API tokens

An API token lets CI or a platform you build drive Luna without a person. It reaches the control API, where projects, environments, deploys and secrets are managed.

An API token holds only the scopes you give it, such as `deploy:write` or `logs:read`. It reaches one project, or with `--org` every project of an organisation. A project's token can be limited to some of its environments.

```bash
luna tokens create --name ci --scope deploy:write --env staging
```

A token can never do more than the person who made it. It cannot manage members or change billing.

## Account keys

An account key acts as you, with your roles in each organisation. `luna login` makes one for the machine and keeps it in `~/.luna/credentials.json`. `luna logout` revokes it.

You type your password in the browser, never in the CLI.

`luna tokens create --account` makes an account key for a script that must act as you. It is read-only unless you add `--write`.

## Who is calling

Inside a handler, `ctx.caller` says where a call came from.

| `ctx.caller` | The call came from |
| --- | --- |
| `{ external }` | Outside Luna. It also has `connection` when the call came over an open connection |
| `{ luna, key }` | Another luna, or a stateless luna type with an empty `key` |
| `{ schedule: true }` | A schedule |
| `{ system: true }` | Luna itself |

Luna does not sign your app's users in. A luna type can check each outside call itself in its `onBeforeConnect` hook, and what that hook returns becomes `ctx.caller.external`.
