Secrets and backends
Keep API keys out of the browser, and run server-side work for your Shell app in a Coderunner.
Everything in a Shell app runs in your users' browsers, and anyone can read its code. So a Shell app must never hold a secret: no API keys, no passwords, no AI model keys. Work that needs a secret, or must run on a server, goes in a backend: a Coderunner that your app calls.
When you need a backend
- Calling a service that needs an API key: payments, email, a third-party API.
- Calling AI models through the LLM Gateway (coming soon), which needs your workspace's key.
- Logic users mustn't be able to see or change: pricing, approvals, anything that checks permissions itself.
Reading and writing your own data, files and flags doesn't need a backend: your app does that directly with the user's Shell site token.
Build the backend
- Deploy a Service with an HTTP API: your own server, in any language.
- Store its secrets in Data → Secrets, and have it read them at runtime with an M2M token. See Configuration and secrets.
- Call it from your app with an app token for the signed-in user. Use the backend's own domain if you've given it one (see Domains), otherwise its Deployment URL:
import { getAppToken } from "@groundfloorcloud/app-auth/browser";
const res = await fetch("https://api.example.com/api/checkout", {
method: "POST",
headers: {
Authorization: `Bearer ${await getAppToken()}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ planId }),
});getAppToken() is rolling out, in @groundfloorcloud/app-auth 0.2.0 and later, and needs Groundfloor sign-in; see Get a token for your own backend. Until you switch, sending the Shell site token (await window.__GF_GET_ACCESS_TOKEN__()) still works: backends that check it keep working until Groundfloor announces a date to retire it. With External sign-in, send your provider's token, which is the Shell site token.
Your Shell site and the backend are on different addresses, so the browser blocks this call unless the backend allows your Shell site's address (CORS). Each environment has its own Shell site (rolling out), so allow the dev and staging sites too if their apps call this backend. See Allow calls from your Shell site.
- In the backend, check the token on every request: anyone can call a Service directly, signed in or not. Check an app token shows what to check, and how to accept the Shell site token as well while you move over; Who can reach your Service covers the Shell site token, with examples for FastAPI and Node.js.
The backend calls Groundfloor with its own M2M token, not the user's, so it can only do what you've allowed that M2M client to do.
AI models
Call AI models from the backend, not the app. Once the LLM Gateway is available (it's coming soon), store your workspace's LLM Gateway key in Secrets as LITELLM_API_KEY, read it at runtime, and send requests to the gateway with any OpenAI-compatible client. See LLM Gateway for the key and the available models.
Next steps
- Services — deploy the backend.
- M2M tokens — credentials for the backend.
- Secrets — store and rotate secrets.