> ## Documentation Index
> Fetch the complete documentation index at: https://www.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom Functions

> Write standalone functions that use DynamicClient services and follow the Dynamic JavaScript SDK conventions.

Not every extension needs to register a provider. A custom function is the simplest way to add behavior: it follows the SDK's standard function shape and can access `DynamicCore` services through the client.

## Conventions

Dynamic SDK functions use a consistent shape:

* The first argument is a single object with named parameters.
* The second argument is an optional `DynamicClient`, defaulting to `getDefaultClient()`.
* Use named exports and match the file name to the function name.

## Example: call an external API

This function uses `getCore` to access the client's `fetch` service and talk to your own backend.

```typescript theme={"system"}
import { getCore, getDefaultClient } from "@dynamic-labs-sdk/client/core";
import type { DynamicClient } from "@dynamic-labs-sdk/client";

type FetchMyDataParams = {
  userId: string;
};

type FetchMyDataResult = {
  balance: string;
};

export const fetchMyData = async (
  { userId }: FetchMyDataParams,
  client: DynamicClient = getDefaultClient()
): Promise<FetchMyDataResult> => {
  const response = await getCore(client).fetch(
    `https://api.example.com/users/${userId}`
  );

  if (!response.ok) {
    throw new Error(`My API request failed: ${response.status}`);
  }

  return response.json();
};
```

## Use the function

Import the function from your extension package. The default client is used automatically:

```typescript theme={"system"}
import { fetchMyData } from "my-custom-extension";

const data = await fetchMyData({ userId: "123" });
```

When your app uses multiple Dynamic clients, create the client in one module and pass it as the second argument:

```typescript theme={"system"}
// dynamicClient.ts
import { createDynamicClient } from "@dynamic-labs-sdk/client";

export const dynamicClient = createDynamicClient({
  environmentId: "YOUR_ENVIRONMENT_ID",
});
```

```typescript theme={"system"}
import { dynamicClient } from "./dynamicClient";
import { fetchMyData } from "my-custom-extension";

const data = await fetchMyData({ userId: "123" }, dynamicClient);
```
