JavaScript / TypeScript

Install and configure @visgate_ai/client for Node.js, browsers, and frameworks.

Installation

npm install @visgate_ai/client

Works in Node.js (18+), browsers, and with React, Vite, Next.js, and vanilla JS.

Basic Usage

import { Client } from "@visgate_ai/client";
 
const client = new Client(); // reads VISGATE_API_KEY from env
 
const result = await client.generate("a sunset over Istanbul");
console.log(result.imageUrl, result.cost, result.provider);
 
client.close();

Configuration

const client = new Client({
  apiKey: "vg-...",                        // or process.env.VISGATE_API_KEY
  baseUrl: "https://visgateai.com/api/v1", // default
  timeout: 120000,                         // 120s default
  maxRetries: 2,                           // retries for 429 and 5xx
});

Browser / Proxy Mode

For browser apps, use a server proxy to keep your API key secure:

const client = new Client({ proxyUrl: "/api/visgate" });

No apiKey needed — the proxy injects the key on the server. See Server Proxy for setup.

Session Auth

When using session tokens (e.g. Firebase), pass a getToken function:

const client = new Client({
  proxyUrl: "/api/visgate",
  getToken: async () => {
    return (await auth.currentUser?.getIdToken()) ?? "";
  },
});

If you use httpOnly cookies, do not use getToken. Use Client({ proxyUrl: "/api/visgate" }) and ensure requests send credentials so the cookie is included.

AsyncClient

For API parity with the Python SDK, AsyncClient is also exported with the same interface:

import { AsyncClient } from "@visgate_ai/client";
 
const client = new AsyncClient();
const result = await client.generate("a sunset");

Features

  • Promise-based API — all methods return Promises; use async/await or .then()
  • Automatic retries — transient errors (429, 5xx) retried with exponential backoff
  • Typed exceptions — catch AuthenticationError, RateLimitError, ProviderError, etc.
  • Full TypeScript support — type definitions included
  • Framework-agnostic — works in vanilla JS, React, Vite, Next.js

On this page