xcladeDocs
Storefront GraphQL

Client Libraries & SDK

Connect with @xclade/sdk v2, native fetch, urql, Apollo Client, or graphql-request

Client Libraries & SDK

@xclade/sdk v2.0 is a zero-dependency TypeScript SDK built specifically for xclade's Go GraphQL endpoints (/graphql and /admin/graphql):

pnpm add @xclade/sdk
import { createXcladeSDK } from '@xclade/sdk';

export const xclade = createXcladeSDK({
  baseUrl: 'https://{shortId}.api.myxclade.com',
  publishableKey: 'pk_live_...',
});

const { products } = await xclade.store.listProducts({
  limit: 12,
  currency_code: 'usd',
});

See the full @xclade/sdk guide for Storefront, B2B, and Merchant Admin usage.


2. Zero-Dependency fetch (Next.js Server Components)

export async function xcladeGraphQL<T>(
  query: string,
  variables?: Record<string, unknown>,
  options?: { token?: string; locale?: string; revalidate?: number },
): Promise<T> {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
    'X-Xclade-Publishable-Key': process.env.NEXT_PUBLIC_XCLADE_PUBLISHABLE_KEY!,
    'X-Xclade-Request': 'true',
  };

  if (options?.token) {
    headers.Authorization = `Bearer ${options.token}`;
  }
  if (options?.locale) {
    headers['X-Xclade-Locale'] = options.locale;
  }

  const res = await fetch(`${process.env.NEXT_PUBLIC_XCLADE_API_URL}/graphql`, {
    method: 'POST',
    headers,
    body: JSON.stringify({ query, variables }),
    next: options?.revalidate !== undefined ? { revalidate: options.revalidate } : undefined,
  });

  const payload = await res.json();
  if (payload.errors?.length) {
    const first = payload.errors[0];
    throw new Error(`[${first.extensions?.code ?? 'GRAPHQL_ERROR'}] ${first.message}`);
  }
  return payload.data as T;
}

3. urql with Automatic Persisted Queries (APQ)

xclade's Go gqlgen server supports Automatic Persisted Queries (APQ) (512-entry LRU hash cache) and GET /graphql queries for CDN caching. Always include X-Xclade-Publishable-Key and X-Xclade-Request: true so GET requests pass CSRF preflight:

import { Client, cacheExchange, fetchExchange } from '@urql/core';
import { persistedExchange } from '@urql/exchange-persisted';

export const urqlClient = new Client({
  url: 'https://{shortId}.api.myxclade.com/graphql',
  fetchOptions: () => ({
    headers: {
      'X-Xclade-Publishable-Key': process.env.NEXT_PUBLIC_XCLADE_PUBLISHABLE_KEY!,
      'X-Xclade-Request': 'true',
    },
  }),
  exchanges: [
    cacheExchange,
    persistedExchange({
      preferGetForPersistedQueries: true,
    }),
    fetchExchange,
  ],
});

4. graphql-request

import { GraphQLClient } from 'graphql-request';

export const gql = new GraphQLClient('https://{shortId}.api.myxclade.com/graphql', {
  headers: {
    'X-Xclade-Publishable-Key': process.env.NEXT_PUBLIC_XCLADE_PUBLISHABLE_KEY!,
    'X-Xclade-Request': 'true',
  },
});

On this page