Package your own reusable data logic behind a typed API on the SDK instance by authoring a custom extension. With an extension, you can:
Wrap repeated GraphQL queries, a caching layer, a custom API client, telemetry, and so on into one reusable namespace instead of duplicating them.
Get full type safety via sdk.ext for each extension’s return type. Duplicate names or non-literal names are rejected at compile time.
Isolate your capabilities from SDK evolution under the sdk.ext namespace, so you get future SDK changes easily.
Define an Extension
Define a custom extension with defineDataExtension. Provide a name for your extension, and a setup function that receives the resolved DataSDK instance. setup can be synchronous or asynchronous, so you can await a dynamic import or a GraphQL query before you return the API.
1import{createDataSDK}from "@salesforce/platform-sdk/data";2import{defineDataExtension, type DataSDK}from "@salesforce/platform-sdk";34interface AccountApi{5 getName(recordId: string): Promise<string | undefined>;6}78const accountExtension = defineDataExtension({9 name: "account",10 setup:(sdk: DataSDK): AccountApi =>({11 async getName(recordId){12 const result = await sdk.graphql?.query<{uiapi:{query:{Account:{edges:{node:{Name:{value: string}}}[]}}}}>({13 query: GET_ACCOUNT_NAME,14 variables:{recordId},15});16 return result?.data?.uiapi.query.Account.edges[0]?.node.Name.value;17},18}),19});2021// Attach the extension, then call it under sdk.ext22const sdk = await createDataSDK({extensions:[accountExtension]});23const name = await sdk.ext.account.getName("001xx000003DGb2AAG");
Pass the extensions array inline, as in the example, so that the SDK types sdk.ext.<name> for each extension you attach. If you assign the array to a variable first, add as const to keep the sdk.ext types.
defineDataExtension Fields
Field
Type
Description
name
string
The name to refer to the extension using the sdk.ext.<name> format.
setup
(sdk: DataSDK) => Api | Promise<Api>
Builds the extension API. Runs when you create the SDK instance, with the resolved DataSDK passed in. Returns the API object under name.
minApiVersion
string
Optional. The earliest Salesforce API version the extension supports, such as "64.0". Specify an API version when the extension depends on a data shape or capability from a specific API version. See Set a Minimum API Version.
Attach an Extension
Attach extensions by passing them to createDataSDK() in the extensions array. The SDK builds each extension’s API when you create the instance, so the extensions are ready to use with createDataSDK(). Call an extension’s API under the SDK’s ext namespace using sdk.ext.<name>.
1import{createDataSDK}from "@salesforce/platform-sdk/data";2import{labels}from "@salesforce/platform-sdk/data/extensions";34const sdk = await createDataSDK({extensions:[labels({namespace: "MyApp"})]});56// Call the extension API under sdk.ext7const greeting = await sdk.ext.labels.get?.("Greeting_Label");
Pass the extensions array inline, as in the example, so that the SDK types sdk.ext.<name> for each extension you attach. If you assign the array to a variable first, add as const to keep the sdk.ext types.
Specify Unique API Names
Add each extension using a unique API name. If two extensions use the same name, attach one with an alias so each has a distinct key. Pass { extension, as } instead of the bare extension, and call the aliased extension under its as key.
An extension can declare a minApiVersion, the earliest Salesforce API version it supports. When an extension declares minApiVersion, createDataSDK() checks it against the API version the SDK resolves for the surface. If the resolved version is lower than the extension’s minApiVersion, createDataSDK() rejects with an error and doesn’t attach any extension in the array.
1const sdk = await createDataSDK({2 extensions:[labels()], // requires API version 64.0 or later3});