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

# LangChain.js

> Give your LLM agents the ability to generate and edit images with Decart.

The `@decartai/langchain` package provides a [LangChain Tool](https://js.langchain.com/docs/concepts/tools) that gives any LLM agent the ability to generate and edit images using Decart.

## Prerequisites

* A [Decart API key](/getting-started/authentication)
* Node.js 18+
* An LLM provider (Anthropic, OpenAI, etc.) for agent usage

## Installation

Install the package along with the required `@langchain/core` peer dependency:

<CodeGroup>
  ```bash npm theme={null}
  npm install @decartai/langchain @langchain/core
  ```

  ```bash pnpm theme={null}
  pnpm add @decartai/langchain @langchain/core
  ```

  ```bash yarn theme={null}
  yarn add @decartai/langchain @langchain/core
  ```
</CodeGroup>

## Setup

Set your Decart API key as an environment variable:

```bash theme={null}
export DECART_API_KEY="your-api-key"
```

Get your API key from [platform.decart.ai](https://platform.decart.ai). See [authentication](/getting-started/authentication) for details.

Or pass your API key directly to the tool:

```typescript theme={null}
import { DecartImageTool } from '@decartai/langchain';

const tool = new DecartImageTool({ apiKey: 'your-api-key' });
```

## Quick start

### Text-to-image

Generate an image from a text prompt:

```typescript theme={null}
import { DecartImageTool } from '@decartai/langchain';

const tool = new DecartImageTool();

const image = await tool.invoke({
  prompt: 'A serene mountain landscape at sunset',
  resolution: '720p',
});
// Returns: data:image/png;base64,...
```

### Image editing

Edit an existing image by providing an `imageUrl`:

```typescript theme={null}
const editedImage = await tool.invoke({
  prompt: 'Change the sky to aurora borealis',
  imageUrl: 'https://example.com/original.jpg',
});
```

<Tip>
  The tool automatically selects the right model: `lucy-pro-t2i` for text-to-image (no `imageUrl`), and `lucy-image-2` for image-to-image editing (with `imageUrl`).
</Tip>

## Using with LLM agents

Bind the tool to an LLM to let it generate images on its own:

```typescript theme={null}
import { ChatAnthropic } from '@langchain/anthropic';
import { HumanMessage } from '@langchain/core/messages';
import { DecartImageTool } from '@decartai/langchain';

const llm = new ChatAnthropic({ model: 'claude-sonnet-4-20250514' });
const tool = new DecartImageTool();

const llmWithTools = llm.bindTools([tool]);

const response = await llmWithTools.invoke([
  new HumanMessage('Generate an image of a futuristic city at night'),
]);

if (response.tool_calls?.length > 0) {
  const result = await tool.invoke(response.tool_calls[0].args);
  console.log('Generated image:', result);
}
```

<Tip>
  This works with any LangChain-compatible LLM that supports tool calling — including OpenAI, Google, and Mistral models.
</Tip>

## Parameters

<ParamField body="prompt" type="string" required>
  Text description for image generation, or an edit instruction for image-to-image.
</ParamField>

<ParamField body="resolution" type={'"480p" | "720p"'} default="720p">
  Output resolution.
</ParamField>

<ParamField body="orientation" type={'"landscape" | "portrait"'}>
  Output orientation. Only applies to text-to-image generation.
</ParamField>

<ParamField body="seed" type="number">
  Seed value for reproducible results.
</ParamField>

<ParamField body="imageUrl" type="string">
  Source image URL for image-to-image editing. When provided, the tool uses `lucy-image-2` instead of `lucy-pro-t2i`.
</ParamField>

<ParamField body="enhancePrompt" type="boolean" default="true">
  Automatically enhance the prompt for better results.
</ParamField>

## Model selection

The tool selects the model automatically based on your input:

| Input                 | Model          | Mode                     |
| --------------------- | -------------- | ------------------------ |
| `prompt` only         | `lucy-pro-t2i` | Text-to-image generation |
| `prompt` + `imageUrl` | `lucy-image-2` | Image-to-image editing   |

## Constructor options

<ParamField body="apiKey" type="string">
  Decart API key. Defaults to the `DECART_API_KEY` environment variable.
</ParamField>

<ParamField body="baseUrl" type="string">
  Custom API base URL.
</ParamField>

## Next steps

<CardGroup cols={3}>
  <Card title="Models overview" icon="layer-group" href="/getting-started/models">
    Explore all available Decart models.
  </Card>

  <Card title="JavaScript SDK" icon="js" href="/sdks/javascript">
    Use Decart's native SDK for full API access.
  </Card>

  <Card title="GitHub" icon="github" href="https://github.com/DecartAI/langchain-js-decart">
    View source code and contribute.
  </Card>
</CardGroup>
