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

# JavaScript

> Integrate ZeroGPU into your JavaScript or Node.js application.

## Official SDK (recommended)

Install the published client from [npm](https://www.npmjs.com/package/zerogpu-api):

```bash theme={null}
npm install zerogpu-api
```

```javascript theme={null}
import { ZerogpuApiClient } from 'zerogpu-api';

const client = new ZerogpuApiClient({
  apiKey: process.env.ZEROGPU_API_KEY,
  projectId: process.env.ZEROGPU_PROJECT_ID,
});

// Many models accept plain string `input`; others accept a message list: see API reference.
const response = await client.responses.createResponse({
  model: process.env.ZEROGPU_MODEL ?? 'YOUR_MODEL',
  input: 'Your input text here...',
  text: { format: { type: 'text' } },
});

console.log(response.output);
```

Source and regeneration workflow: [zerogpu/SDK](https://github.com/zerogpu/SDK) on GitHub.

### Chat completions (`/v1/chat/completions`)

For models that use the chat route, call `client.chat.createChatCompletion({ model, messages, metadata? })`. See [Chat completions](/api-reference/chat-completions).

### Responses with `metadata`

Optional `metadata` on `createResponse` is supported for model-specific options (e.g. PII). See [Responses](/api-reference/responses).

***

## Raw HTTP (`fetch`)

If you prefer not to use the SDK, call the endpoint directly.

```javascript theme={null}
const url = 'https://api.zerogpu.ai/v1/responses';
const headers = {
  'content-type': 'application/json',
  'x-api-key': 'YOUR_API_KEY',
  'x-project-id': 'YOUR_PROJECT_ID'
};
const payload = {
  model: 'YOUR_MODEL',
  input: [
    {
      role: 'user',
      content: 'Your input text here...'
    }
  ],
  text: {
    format: {
      type: 'text'
    }
  }
};

fetch(url, {
  method: 'POST',
  headers,
  body: JSON.stringify(payload)
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));
```

## Using environment variables (Node.js)

```javascript theme={null}
const url = 'https://api.zerogpu.ai/v1/responses';
const headers = {
  'content-type': 'application/json',
  'x-api-key': process.env.ZEROGPU_API_KEY,
  'x-project-id': process.env.ZEROGPU_PROJECT_ID
};
const payload = {
  model: 'YOUR_MODEL',
  input: [
    {
      role: 'user',
      content: 'Your input text here...'
    }
  ],
  text: {
    format: {
      type: 'text'
    }
  }
};

const response = await fetch(url, {
  method: 'POST',
  headers,
  body: JSON.stringify(payload)
});

const data = await response.json();
console.log(data.output[0].content[0].text);
```

## Error handling

```javascript theme={null}
try {
  const response = await fetch(url, {
    method: 'POST',
    headers,
    body: JSON.stringify(payload)
  });

  if (!response.ok) {
    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
  }

  const data = await response.json();
  console.log(data.output[0].content[0].text);
} catch (error) {
  console.error('Request failed:', error.message);
}
```
