Node.js Integration

Back to Docs Page

Raw markdown source view for LLM workflows.

---
title: Node.js Integration
description: A lightweight Node.js client pattern for authenticated GameQuery requests.
---

## Minimal API client

```ts
type GroupedServers = {
  game_id: string;
  servers: string[];
};

const GQ_API_BASE_URL = process.env.GQ_API_BASE_URL ?? 'https://api.gamequery.dev';
const GQ_API_VERSION = process.env.GQ_API_VERSION ?? 'v1';

function getHeaders() {
  return {
    'Content-Type': 'application/json',
    'X-API-Token': process.env.GQ_API_TOKEN ?? '',
    'X-API-Token-Type': process.env.GQ_API_TOKEN_TYPE ?? 'FREE',
    'X-API-Token-Email': process.env.GQ_API_TOKEN_EMAIL ?? '',
  };
}

async function gqRequest<T>(path: string, init: RequestInit = {}): Promise<T> {
  const res = await fetch(`${GQ_API_BASE_URL}/${GQ_API_VERSION}${path}`, {
    ...init,
    headers: {
      ...getHeaders(),
      ...(init.headers ?? {}),
    },
  });

  if (!res.ok) {
    const body = await res.text();
    throw new Error(`GameQuery request failed (${res.status}): ${body}`);
  }

  return res.json() as Promise<T>;
}

export async function getGamesCatalog() {
  const res = await fetch(`${GQ_API_BASE_URL}/${GQ_API_VERSION}/get/games`);

  if (!res.ok) {
    const body = await res.text();
    throw new Error(`GameQuery request failed (${res.status}): ${body}`);
  }

  return res.json() as Promise<Array<{ id: string; name: string }>>;
}

export function fetchServers(servers: GroupedServers[]) {
  return gqRequest('/post/fetch', {
    method: 'POST',
    body: JSON.stringify({ servers }),
  });
}
```

## Suggested production hardening

- Add timeout + retry with jitter.
- Log `_meta.invalid_servers` and `_meta.inserted_servers` as structured events.
- Validate `game_id` input with `GET /v1/get/games` and surface API validation errors to operators.
- Keep `GQ_API_VERSION` environment-driven so cutover to `v2` does not require code changes.