|
| 1 | +--- |
| 2 | +title: Cloudflare API Token |
| 3 | +description: Set up the Cloudflare token SST uses for deployments. |
| 4 | +--- |
| 5 | + |
| 6 | +SST needs a Cloudflare API token and account ID before it can deploy to Cloudflare. |
| 7 | + |
| 8 | +Use a Cloudflare [account token](https://developers.cloudflare.com/fundamentals/api/get-started/account-owned-tokens/). It works better for automation because it is not tied to one user. |
| 9 | + |
| 10 | +Before you run `sst dev` or `sst deploy`, set: |
| 11 | + |
| 12 | +```bash |
| 13 | +export CLOUDFLARE_API_TOKEN=aaaaaaaa_aaaaaaaaaaaa_aaaaaaaa |
| 14 | +export CLOUDFLARE_DEFAULT_ACCOUNT_ID=aaaaaaaa_aaaaaaaaaaaa_aaaaaaaa |
| 15 | +``` |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## 1. Create the token |
| 20 | + |
| 21 | +In the Cloudflare dashboard, go to **Manage Account** > **API Tokens** and create an **Account token**. |
| 22 | + |
| 23 | +Start with the **Edit Cloudflare Workers** template. Then add these permissions. |
| 24 | + |
| 25 | +- **Account - D1 - Edit** |
| 26 | +- **Zone - DNS - Edit** |
| 27 | + |
| 28 | +If your app uses other Cloudflare products, add the permissions those features need. |
| 29 | + |
| 30 | +If you use Hyperdrive, also add: |
| 31 | + |
| 32 | +- **Account - Hyperdrive - Edit** |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## 2. Scope the token |
| 37 | + |
| 38 | +Give the token access to the account you want SST to manage. |
| 39 | + |
| 40 | +If you are using Cloudflare DNS with SST, include the zones SST should update. |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## 3. Set the account ID |
| 45 | + |
| 46 | +Set `CLOUDFLARE_DEFAULT_ACCOUNT_ID` to the Cloudflare account ID SST should use. |
| 47 | + |
| 48 | +If you leave it unset, SST falls back to the first account Cloudflare returns for that token. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## 4. Add the credentials to your environment |
| 53 | + |
| 54 | +Set these variables in your shell, `.env`, CI, or SST Console environment before you deploy. |
| 55 | + |
| 56 | +:::note |
| 57 | +Do not store these with `sst secret` for the initial Cloudflare deploy. SST reads them before it can create resources. |
| 58 | +::: |
0 commit comments