Load translations at runtime
Most apps pull their translations at build time with lingo pull and bundle them. If copy changes must reach users without a redeploy, a website or back end can instead have its server fetch bundles from Lingo while it runs and keep them in memory. An edit in Lingo is returned by the next request: there is no publish step.
Fetch from your server only. The API key is a secret, and the API does not send CORS headers, so browsers can't call it. Pass the loaded translations to the client yourself, for example through your framework's page props.
Create a read-only key
In the project, open Settings → API keys, give the key a Name, tick only read:translations, and click Create key. Copy it right away; it is shown once. Store it in your server's environment, never in client code or in git.
The same key works for lingo pull when you build fallback files (see below).
Request a bundle
GET https://<your-lingo-host>/api/v1/projects/{project}/bundles/{lang}/{namespace}
Authorization: Bearer <your-key>
{project}: the project's slug (as in the app's URLs) or its id.{lang}: one of the project's languages. Case and_vs-don't matter, and a bare language works when exactly one project language has it:zh_cnfindszh-CN, andptfindspt-PTif that is the project's only Portuguese. The language used is returned in theContent-Languageheader. An unknown code, or a bare language that matches more than one (zhwhen the project haszh-CNandzh-TW), returns 404 with the project's languages in the message. If your app asks with bare codes, don't add a second variant of one of those languages to the project (pt-BRnext topt-PT):ptwould start returning 404. Switch the app to full codes first.{namespace}: a namespace path, such ascommonorcheckout.payment. The bundle contains that namespace's keys and its sub-namespaces' keys, written relative to it:checkout.paymentturnscheckout.payment.summary.titleintosummary.title. Use_rootfor keys that have no namespace. A namespace that doesn't exist returns 404.
Leave the namespace out, GET …/bundles/{lang}, to get every key of the language under its full path. That is the same content as a lingo pull file.
Options
| Query | Values | Default |
|---|---|---|
format | nested builds objects from dotted keys. flat keeps "a.b": "…" as written. i18next with its default key separator reads both. Libraries that read nested messages, such as next-intl, need nested. Only an app that turns i18next's key separator off needs flat. | nested |
status | translated: every value that has text, including drafts and unreviewed machine translations, like lingo pull. approved: reviewed values only. Values that an import creates arrive as drafts, so right after a first import this returns {} until they are approved, for example by an admin or owner with Set status on the selected keys (see Review). | translated |
Any other value returns 422.
Response
The body is the translations object itself, ready for i18next and similar libraries:
{ "title": "Your bookings", "footer": { "help": "Help" } }
Keys with no translation in that language are left out, so your library falls back to its fallback language instead of showing an empty string. A namespace with nothing translated yet returns {}.
Errors use application/problem+json: 401 for a missing or invalid key, 403 for a key without read:translations, 404 and 422 as above, and 429 when the rate limit is reached.
Keep it fresh and cheap
- Each response carries an
ETag. Send it back asIf-None-Matchon the next request: if nothing changed, the answer is 304 with no body. - Refresh every few minutes rather than every few hours. Each refresh costs languages × namespaces requests (20 languages × 5 namespaces = 100), and a key can make 600 requests per minute by default.
- When a request fails or times out, keep serving the last bundle you had.
async function loadBundle(lang: string, ns: string, etag?: string) {
const res = await fetch(`https://<your-lingo-host>/api/v1/projects/my-site/bundles/${lang}/${ns}`, {
headers: {
Authorization: `Bearer ${process.env.LINGO_API_KEY}`,
...(etag ? { 'If-None-Match': etag } : {}),
},
signal: AbortSignal.timeout(5000),
});
if (res.status === 304) return { unchanged: true };
if (!res.ok) return null; // keep the cached bundle
return { resources: await res.json(), etag: res.headers.get('ETag') ?? undefined };
}
See what your servers download
An admin or owner can open Settings → Statistics in the organisation. It counts every call made with your API keys, per day: how many calls, how many translation keys and how much data, by project, namespace, consumer (the API key), language, operation (the endpoint) and status. A 304 counts as a call with no keys and no data. A call with a missing, invalid, expired or revoked key is not counted. For each API key it also shows the busiest minute against the key's rate limit, so a server that refreshes too often shows up before it gets 429s. Days are in UTC, and the last 90 days are kept.
Fallback files for cold starts
So the server has translations before its first request succeeds, write the same bundles to disk at build time. With CLI 0.4.0 or later, lingo pull can write one file per namespace and name the folders with your app's language codes:
{
"projectId": "my-site",
"apiUrl": "https://<your-lingo-host>",
"outDir": "public/locales",
"split": "namespace",
"languageMap": { "en-GB": "en", "de-DE": "de", "zh-CN": "zh_cn", "pt-PT": "pt" }
}
languageMap needs an entry for every project language that your app names differently. A language you leave out keeps Lingo's code as its folder name: de-DE, where an app that asks for de will not find it.
This writes public/locales/{lang}/{namespace}.json with exactly what the endpoint returns for that language and namespace, plus _root.json for keys without a namespace. Every language gets a file for every namespace. See CLI for all options.