Polinations.AI / GPT4Free.js Client Documentation

Overview

This JavaScript library provides a flexible interface to interact with multiple AI model providers via unified APIs. It supports:


Installation

HTML (CDN)

<script type="module">
  import { Client, PollinationsAI, DeepInfra, Together, Puter, HuggingFace } from 'https://g4f.dev/dist/js/client.js';
</script>

NPM

npm install @gpt4free/g4f.dev

Providers

You can initialize a client using one of the following providers:

import { Client, PollinationsAI, DeepInfra, Together, Puter, HuggingFace } from '@gpt4free/g4f.dev';

// Pollinations
const client = new PollinationsAI({ apiKey: 'optional' });

// DeepInfra
const client = new DeepInfra({ apiKey: 'optional' });

// HuggingFace
const client = new HuggingFace({ apiKey: 'required' });

// Together.AI
const client = new Together({ apiKey: 'optional' });

// Puter
const client = new Puter();

// Custom (e.g., local GPT4Free instance)
const client = new Client({ baseUrl: 'http://localhost:8080/v1', apiKey: 'secret' });

All clients conform to the same method interfaces for completions, models, and image generation.


Chat Completions

Regular Completion

const result = await client.chat.completions.create({
  model: 'gpt-4.1',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'Tell me a joke.' }
  ]
});

Streaming Completion

const stream = await client.chat.completions.create({
  model: 'gpt-4.1',
  messages: [...],
  stream: true
});

for await (const chunk of stream) {
  console.log(chunk.choices[0]?.delta?.content);
}

Image Generation

const result = await client.images.generate({
  model: 'flux', // Or "gpt-image", "sdxl-turbo"
  prompt: 'A futuristic city skyline at night',
  size: '512x512' // Optional
});

const image = new Image();
image.src = result.data[0].url;
document.body.appendChild(image);

Audio Modality Support

const result = await client.chat.completions.create({
  model: 'gpt-4o-audio',
  messages: [...],
  audio: {
    voice: 'alloy',
    format: 'mp3'
  },
  modalities: ['text', 'audio']
});

Model Listing

const models = await client.models.list();
models.forEach(m => console.log(m.id, m.type));

Multi-Provider Web UI Example

You can create a browser-based chat UI supporting multiple providers and models. See pro.html for a working example.

Features:


Configuration Options

Option Type Description Default
baseUrl string Override API base URL Depends on provider
apiKey string Authorization key/token undefined
extraHeaders object Custom headers {}
modelAliases object Shortcut names for models [See below]

Default Model Aliases

Alias Maps To
gpt-4.1 openai-large
gpt-4.1-mini openai
deepseek-v3 deepseek

Error Handling

Client throws informative errors for:


Notes