# Connect PlanetScale

Costfluent reads your PlanetScale spend from your organization's invoices and their line items,
through the PlanetScale API, with a service token you create for it. One connection is one
PlanetScale organization, and the cost of its databases, branches and organization-wide items such as
single sign-on.

## How Costfluent connects

Costfluent lists the organization's invoices, keeps those whose billing period starts in the months
it collects, and reads their line items. A service token is created inside one organization, so you
name the organization beside it; the name is the connection's identity once the token has read it.

Every request carries the token's ID and the token together as one `Authorization` header, the form
PlanetScale accepts from every service token whatever its age. Costfluent stores both encrypted and
uses them on `api.planetscale.com` for three operations only:

1. `GET https://api.planetscale.com/v1/organizations/{organization}`, the organization, of which
   Costfluent keeps only its name; its billing email is never read or stored.
2. `GET https://api.planetscale.com/v1/organizations/{organization}/invoices`, every invoice of the
   organization, page by page.
3. `GET https://api.planetscale.com/v1/organizations/{organization}/invoices/{id}/line-items`, the
   line items of one invoice.

Reading invoices changes nothing in your databases or your billing. The token needs the organization
accesses `read_organization` and `read_invoices` and nothing else
([Service tokens](https://planetscale.com/docs/api/reference/service-tokens), read 2026-11-26).

:::note
Only an Organization Administrator can grant `read_invoices`: PlanetScale lets you add a service
token only the accesses you hold yourself
([Access control](https://planetscale.com/docs/security/access-control), read 2026-11-26).
:::

## Prerequisites

1. The Owner or Integration owner organization role in Costfluent.
2. The Organization Administrator role in the PlanetScale organization, to create the service token
   and grant it `read_invoices`.

## Connect

### Create a service token in PlanetScale

<Stepper>

1. Sign in to the [PlanetScale dashboard](https://app.planetscale.com) and open the organization.
   Its name is the part of the address after `app.planetscale.com/`; note it.
2. Open “Settings”, then “Service tokens”, and create a new service token with a name that names
   Costfluent.
3. Add the organization accesses `read_organization` and `read_invoices` to the token, and no
   database access.
4. Leave the token's IP restrictions empty, which allows requests from every address. Costfluent
   publishes no addresses its requests come from, so a token limited to some addresses refuses them.
5. Copy the token's ID and the token. PlanetScale shows the token only once.

</Stepper>

### Connect in Costfluent

<Stepper>

1. Open **Settings**, then **Integrations**, select **PlanetScale**, and open the **Connect** tab.
2. Select "Add service token", keep the suggested **Display name** or enter your own, and paste the
   **Service token ID**, the **Service token** and the **Organization name**.
3. Select "Test connection" to see each check, or "Connect" to save.

</Stepper>

Before storing anything, Costfluent reads the organization with the token, then the first page of its
invoices. A connection that saves can therefore read your invoices. The organization name is stored as
PlanetScale writes it, whatever its case when you typed it, and a second connection of the same
organization is refused. If PlanetScale does not answer during that check, the connection is saved
anyway and checked again automatically.

## Data and freshness

Each invoice is one month of rows: one row per line item, filed on the first day of the invoice's
billing period. A line item states neither days nor quantities, so the month's cost lands on that day
and daily charts show it there; month and longer views are exact. The cost is in US dollars, the only
currency PlanetScale prices in.

A sync reads every invoice whose billing period starts in the months it collects, by default once a
day, and the previous month is read again until the 15th of the month after it, which collects a
month's invoice once PlanetScale issues it. Where PlanetScale already lists the running month's
invoice, it is read whole at every sync, and when PlanetScale issues it under another ID, its lines
replace the earlier ones.

Each invoice is held to its total. What the total holds beyond the lines PlanetScale bills itself,
such as tax, a coupon or rounding, becomes one row with the charge description "Invoice adjustment",
so a month adds up to the invoice's total. PlanetScale does not say whether a total holds the lines
Cloudflare bills: where the total less PlanetScale's own lines is exactly the sum of those lines, no
adjustment is written, and the month adds up to the total less them; otherwise the whole difference
is the adjustment.

:::caution
A database created through Cloudflare is billed on your Cloudflare invoice, so Costfluent leaves its
lines out of PlanetScale's rows. Its cost reaches Costfluent only through a Cloudflare connection, and
only if Cloudflare's billable usage lists it, which Cloudflare does not say; an Enterprise contract's
usage is not there at all ([Connect Cloudflare](/connect/cloudflare)). Its per-database detail stays
in PlanetScale's dashboard.
:::

Connecting imports as far back as your plan's backfill reaches, and at most six months
([Manage connections](/connect/manage-connections#how-it-behaves)). PlanetScale states no limit on how
far back its invoices reach.

## Reporting dimensions

- **Service** is PlanetScale, with the **Service category** Databases for a line of a database and
  Other for a line of the organization, such as single sign-on, or for the adjustment.
- **Sub-account** is the database, by its ID and name.
- **Resource** is the branch the line bills, named `database/branch`.
- The **SKU** is the line's billable item as PlanetScale names it, such as `ps_10`, and the charge
  description is the line's description.
- **Tag** holds `planetscale:database` and `planetscale:branch`, the database's and the branch's
  names, which Costfluent writes and which do not count as your tags in tag coverage.
- The charge category is usage, credit for a negative line such as a prorated discount, and
  adjustment for the invoice's remainder.
- The billing account is the organization, the invoice issuer PlanetScale, and each row names its
  invoice.

## Update or disconnect

### After connecting

The **Manage** tab lists the connection by its display name. Open the gear icon beside it to see
whether the invoices can still be read, which workspaces the connection feeds, and which months have
been imported. **Run checks** runs the checks again on request.

### Rotating or removing access

PlanetScale does not rotate service tokens for you. Create a new service token with the same two
accesses, select **Update credentials** on the connection page and paste its ID and token with the
same organization name; a token of another organization is refused. Once the connection reads with the
new token, delete the old one under “Settings”, then “Service tokens”.

To retire the connection, remove it in Costfluent, then delete its service token. Removing the
connection erases the cost it imported
([Remove a connection](/connect/manage-connections#remove-a-connection)).

## Troubleshooting

The check's messages say what PlanetScale answered:

- **PlanetScale refused this service token: the service token ID or token is wrong or the token was
  deleted.** Copy both again, or create a new service token, and paste them.
- **PlanetScale refused this service token the organization: the token lacks the read_organization
  access.** Add `read_organization` to the token under “Settings”, then “Service tokens”.
- **This service token cannot see organization …** The organization name is not the one in your
  dashboard address, or the token was created in another organization.
- **PlanetScale refused this service token the organization's invoices: the token lacks
  read_invoices, which only an Organization Administrator can grant.** Ask an Organization
  Administrator to add `read_invoices` to the token.
- **A PlanetScale service token ID never holds a colon.** The token itself was pasted as its ID; paste
  the ID and the token in their own fields.
- **PlanetScale could not be reached to verify these credentials.** PlanetScale did not answer or
  answered with an error of its own. Select **Run checks** later; a connection saved meanwhile is
  checked again automatically.
- **A sync that fails with "Reconnect it with a service token with the read_organization and
  read_invoices accesses".** PlanetScale refused the stored token while collecting: the token was
  deleted or lost an access. Create a new service token and select **Update credentials**.

## From your assistant

With the [Costfluent MCP server](/developers/mcp) connected, your assistant answers these with `query_costs`.

```text
Which PlanetScale databases cost the most over the last three months?
```

```text
How did each PlanetScale branch of mydb cost last month compared with the month before?
```
