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

# Use Fortytwo with the AI SDK

> Call the Fortytwo API from TypeScript with Vercel's AI SDK: install the OpenAI-compatible provider, create a Fortytwo provider, then generate text, stream, call tools and get structured output.

The [AI SDK ](https://ai-sdk.dev/) is a TypeScript toolkit for building AI apps and agents. It reaches the Fortytwo API through its OpenAI-compatible provider, `@ai-sdk/openai-compatible`.

> [!NOTE]
>
> **Before you start:** create a Fortytwo API key and buy credits — see [Quickstart, step 1](https://docs.fortytwo.network/docs/quickstart.md#1-create-an-api-key). Keep the key on your server: the API cannot be called from a web page in the browser.
>
> | Setting | Value |
> | - | - |
> | Base URL | `https://api.fortytwo.network/v1` |
> | Model ID | `qwen/qwen3.8-27b` in this guide, or another ID from [Models](https://docs.fortytwo.network/docs/models.md) |

## Install

```bash npm theme={null}
npm install ai @ai-sdk/openai-compatible zod
```

```bash pnpm theme={null}
pnpm add ai @ai-sdk/openai-compatible zod
```

```bash yarn theme={null}
yarn add ai @ai-sdk/openai-compatible zod
```

```bash bun theme={null}
bun add ai @ai-sdk/openai-compatible zod
```

`zod` is needed only for tools and structured output.

Put your key in an environment variable:

```bash theme={null}
export FORTYTWO_API_KEY="<your Fortytwo key>"
```

## Create the provider

```ts fortytwo.ts theme={null}
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";

export const fortytwo = createOpenAICompatible({
  name: "fortytwo",
  baseURL: "https://api.fortytwo.network/v1",
  apiKey: process.env.FORTYTWO_API_KEY,
  includeUsage: true,
  supportsStructuredOutputs: true,
});

export const model = fortytwo("qwen/qwen3.8-27b");
```

* `includeUsage` adds token counts to streamed responses.
* `supportsStructuredOutputs` sends JSON schemas in strict mode, which the Fortytwo API supports.
* `fortytwo("qwen/qwen3.8-27b")` takes any model ID from [Models](https://docs.fortytwo.network/docs/models.md).

## Generate text

```ts theme={null}
import { generateText } from "ai";
import { model } from "./fortytwo";

const { text, reasoningText, usage } = await generateText({
  model,
  prompt: "Explain prompt caching in two sentences.",
  providerOptions: { fortytwo: { reasoningEffort: "medium" } },
});
```

`reasoningText` holds the model's reasoning. `reasoningEffort` is sent as `reasoning_effort`; the accepted values and the default are in [Models](https://docs.fortytwo.network/docs/models.md).

## Stream

```ts theme={null}
import { streamText } from "ai";
import { model } from "./fortytwo";

const result = streamText({
  model,
  prompt: "Write a haiku about distributed inference.",
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}
```

## Call tools

```ts theme={null}
import { generateText, stepCountIs, tool } from "ai";
import { z } from "zod";
import { model } from "./fortytwo";

const { text } = await generateText({
  model,
  prompt: "What is the weather in London?",
  tools: {
    weather: tool({
      description: "Get the current weather for a city",
      inputSchema: z.object({ city: z.string() }),
      execute: async ({ city }) => ({ city, temperatureC: 12 }),
    }),
  },
  stopWhen: stepCountIs(5),
});
```

`stopWhen` lets the model call a tool, read the result and answer in the same call.

## Get structured output

```ts theme={null}
import { generateText, Output } from "ai";
import { z } from "zod";
import { model } from "./fortytwo";

const { output } = await generateText({
  model,
  prompt: "Give me a vegetarian lasagne recipe.",
  output: Output.object({
    schema: z.object({
      name: z.string(),
      ingredients: z.array(z.string()),
      steps: z.array(z.string()),
    }),
  }),
});
```

## Send an image

```ts theme={null}
import { readFileSync } from "node:fs";
import { generateText } from "ai";
import { model } from "./fortytwo";

const { text } = await generateText({
  model,
  messages: [
    {
      role: "user",
      content: [
        { type: "text", text: "What is on this screenshot?" },
        { type: "file", data: readFileSync("screenshot.png"), mediaType: "image/png" },
      ],
    },
  ],
});
```

Supported formats and limits are in [Images](https://docs.fortytwo.network/docs/images.md).

## Limits

* Chat Completions only: the AI SDK's OpenAI Responses provider (`@ai-sdk/openai` with `openai.responses(...)`) does not work with the Fortytwo API. Use `@ai-sdk/openai-compatible` as above.
* Requests go out with the AI SDK's own `User-Agent`; add a `headers` setting to `createOpenAICompatible` if you want to tag your traffic.

More options: [OpenAI-compatible providers ](https://ai-sdk.dev/providers/openai-compatible-providers).

***

Something not working with your agent, or using one we have not listed? Tell us in the platform: [Feedback](https://docs.fortytwo.network/docs/support.md#ideas-feedback-and-small-bugs) for ideas and small bugs, [Contact support](https://docs.fortytwo.network/docs/support.md#support-account-api-billing-credits-errors) if it blocks your work, or [Report a security issue](https://docs.fortytwo.network/docs/support.md#security-issue).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.