Skip to content
Getting started

TypeScript quickstart

Get started with the Lightfield API using TypeScript.

This guide walks you through making your first request to the Lightfield API using the official TypeScript SDK.

Install the library from npm:

Terminal window
npm install lightfield

An API key can be created in Lightfield settings (admin only).

When creating the key, select the scopes your integration needs.

For the example below, you’ll need accounts:read.

Create a client and list one account to verify your key and scope.

1. Create a new file (e.g. quickstart.ts) and paste in:

import Lightfield from "lightfield";
const client = new Lightfield({
apiKey: "My API Key",
});
async function main() {
const accounts = await client.account.list({ limit: 1 });
console.log(accounts);
}
main();

2. Replace "My API Key" with your actual API key (sk_lf_...).

3. Run the script from your terminal (from the same directory as the file):

Terminal window
npx tsx quickstart.ts

A successful response looks like:

{
"data": [
{
"id": "id",
"createdAt": "createdAt",
"fields": {
"foo": {
"value": "string",
"valueType": "valueType"
}
},
"httpLink": "httpLink",
"relationships": {
"foo": {
"cardinality": "cardinality",
"objectType": "objectType",
"values": [
"string"
]
}
}
}
],
"object": "list",
"totalCount": 1
}

With limit: 1 you get at most one account. To fetch more results, use limit and offset — see List methods for pagination. The full API is in the TypeScript API Reference.

Error handling: The SDK raises typed exceptions you can catch. If your API key is wrong you’ll get a Lightfield.AuthenticationError:

import Lightfield from "lightfield";
const client = new Lightfield({ apiKey: "invalid-key" });
async function main() {
try {
await client.account.list({ limit: 1 });
} catch (err) {
if (err instanceof Lightfield.AuthenticationError) {
console.log(err.status); // 401
console.log(err.message);
}
}
}
main();

If the key is valid but missing the required scope, a Lightfield.PermissionDeniedError (403) is thrown instead.

For more on error responses and how to handle them, see Errors.