Skip to content

Public API Keys in the Browser, Without Regret

A storefront shows products to people who haven't signed in, and a mobile app needs its feature flags before anyone logs in. Both need to call your API with something, and whatever they send is readable by anyone who opens the browser's developer tools. So the question isn't how to hide the key. It's what a copy of it can do.

A key with no secret

ObjectsGrid gives visitors a service account of type public. It has no secret: there's nothing to keep private. An administrator makes its public API key:

curl -X POST https://apis.objectsgrid.com/serviceaccounts/<public service account id>/apikey \
  -H 'X-ObjectsGrid-AccessPoint: EU' -H "Authorization: Bearer $ADMIN_TOKEN"

The page sends it as Authorization: Bearer <key>, with no token request. It has no expiry, because it's public anyway: it works until you disable or delete its account, which stops it within a minute.

Rule 1: grant only what every visitor may see

The key carries its account's tags, such as role = anonymous, and your ABAC policy decides what that role may do. A new Organization Account's default policy is a good start. The anonymous role may:

  • read the public catalog: products, offers, brands, reviews, places, services, events, return and shipping policies;
  • keep shoppers' carts by id, without listing them or checking out;
  • read the configuration and feature flags tagged visibility = public;
  • send frontend logs.

Nothing else: no persons, orders, payments or accounts. Test it by calling with the key: GET /persons must be 401.

Rule 2: lock it to your sites

Another website can copy your key and call your API from its own visitors' browsers. Browsers send an Origin header that page scripts can't change, so list yours on the public service account:

{"allowedOrigins": ["https://shop.example.com", "https://www.shop.example.com"]}

A browser request from any other origin is refused with 403. This stops other websites, not scripts: a server or a command line can send any Origin, or none. That's why rule 1 comes first.

Rule 3: give it its own budget

A script running your key in a loop still costs you requests. Cap the key itself:

{"monthlyRequestLimit": 200000}

Once the key has made that many billable requests in a month, its requests get 429 until the month ends, while your server's keys keep working. It sits under your Organization Account's spending cap, which bounds everything. An administrator sees the key's requests this month by origin:

curl https://apis.objectsgrid.com/serviceaccounts/<id>/usage \
  -H 'X-ObjectsGrid-AccessPoint: EU' -H "Authorization: Bearer $ADMIN_TOKEN"

Requests from an origin you don't recognize, or a surge with no Origin at all, tell you someone is using your key outside your pages.

Rule 4: replace it, don't just stop it

You can't tell a copier from your visitors by the key, so when a key is misused, replace it:

  1. Create a new public service account with the same tags. Your policy decides on tags, so it gets the same access.
  2. Make its key, ship it in your site or app, and deploy.
  3. Disable or delete the old account. Its key stops within a minute.

Both keys work between steps 1 and 3, so visitors see no gap. Old versions of a mobile app keep the old key until people update; the old account's usage shows when its traffic has died down.

What never goes in a page

A secret service account's credentials: its secret, or an access token from it. Those grant what your servers may do. Keep them on your servers, and see rotate API secrets without downtime for how. The authentication guide has the details of public keys.