create-api-client

A setup for one shared client used to communicate with web APIs, which are services that let software exchange data. It includes consistent requests, error handling, and TypeScript types that describe the data.

In plain words
What is it for?
Use it to configure a base URL and headers, handle network and HTTP errors, add typed GET, POST, PUT, and DELETE methods, and manage authentication, cancellation, and retries.
Why use it?
It keeps API communication in one place instead of repeating request and error-handling code throughout the application. The types help catch mismatched data while coding.

Command for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/farzannajipour/cursor-react-rules/create-api-client
Clone the repo
git clone --depth 1 https://github.com/Farzannajipour/cursor-react-rules

Made for: Cursor.

Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 2,037 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 1 finding. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.02037
Opus 5 $0.00000 $0.01019
Sonnet 5 $0.00000 $0.00407
Haiku 4.5 $0.00000 $0.00204

Measured 2d ago against content hash b17f3d25f331, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

create-api-client scanned grade A with 1 finding against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

const response = await fetch(url, config);
.cursor/commands/create-api-client.md · 346 lines

How it starts

The opening of the file, as written. The whole thing — 346 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Create API Client

Overview

Set up a centralized API client with error handling, interceptors, and TypeScript types.

Steps

  1. Create base client

    • Configure base URL
    • Set default headers
    • Add request/response interceptors
  2. Add error handling

    • Handle network errors
    • Handle HTTP errors
    • Format error messages
  3. Create typed methods

    • GET, POST, PUT, DELETE methods
    • TypeScript generics for responses
    • Request/response type safety
  4. Add utilities

    • Authentication token handling
    • Request cancellation
    • Retry logic

Template

Basic API Client

// lib/api-client.ts
class APIClient {
  private baseURL: string;
  private defaultHeaders: HeadersInit;

  constructor(baseURL: string) {
    this.baseURL = baseURL;
    this.defaultHeaders = {
      'Content-Type': 'application/json',
    };
  }

  private async request<T>(
    endpoint: string,
    options: RequestInit = {}
  ): Promise<T> {
    const url = `${this.baseURL}${endpoint}`;
    
    const config: RequestInit = {
      ...options,
      headers: {
        ...this.defaultHeaders,
        ...options.headers,
      },
    };

    try {
      const response = await fetch(url, config);

      if (!response.ok) {
        const error = await response.json().catch(() => ({}));
        throw new APIError(
          error.message || `HTTP ${response.status}: ${response.statusText}`,
          response.status
        );
      }

      // Handle empty responses
      const contentType = response.headers.get('content-type');
      if (contentType && contentType.includes('application/json')) {
        return await response.json();
      }
      
      return {} as T;
    } catch (error) {
      if (error instanceof APIError) {
        throw error;
      }
      throw new APIError('Network error', 0);
    }
  }

  async get<T>(endpoint: string, options?: RequestInit): Promise<T> {
    return this.request<T>(endpoint, { ...options, method: 'GET' });
  }

  async post<T>(endpoint: string, data?: any, options?: RequestInit): Promise<T> {
    return this.request<T>(endpoint, {
      ...options,
      method: 'POST',
      body: JSON.stringify(data),
    });
  }

  async put<T>(endpoint: string, data?: any, options?: RequestInit): Promise<T> {
    return this.request<T>(endpoint, {
      ...options,
      method: 'PUT',
      body: JSON.stringify(data),
    });
  }

  async delete<T>(endpoint: string, options?: RequestInit): Promise<T> {
    return this.request<T>(endpoint, { ...options, method: 'DELETE' });
  }

  setAuthToken(token: string) {
    this.defaultHeaders = {
      ...this.defaultHeaders,
      Authorization: `Bearer ${token}`,
    };
  }

  clearAuthToken() {
    const { Authorization, ...rest } = this.defaultHeaders;
    this.defaultHeaders = rest;
  }
}

class APIError extends Error {
  constructor(message: string, public status: number) {
    super(message);
    this.name = 'APIError';
  }
}

export const apiClient = new APIClient(process.env.NEXT_PUBLIC_API_URL || '/api');

Read the full file on GitHub · 346 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 346 lines · 0 tokens per session scan A b17f3d25f331

Subscribe to this mod's changes

create-api-client is a command published in the GitHub repository Farzannajipour/cursor-react-rules (3 stars, last pushed 7mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 2,037 tokens. A static security scan graded it A with 1 finding (makes network calls). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.