npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@contract-first-api/api-client

v2.1.1

Published

Create a typed runtime API client from a shared contract tree.

Readme

@contract-first-api/api-client

@contract-first-api/api-client turns a shared contract tree into a typed runtime client. It gives you one fetch function per contract, with request and response types inferred from the contract.

What you do with this package

Use it to:

  • create a typed client from shared contracts
  • call contracts with the same request shape the backend expects
  • validate responses against the contract response schema
  • centralize base URL, default headers, and HTTP error handling
  • forward per-request fetch options when you need them

Basic usage

import { ApiClient } from "@contract-first-api/api-client";
import { contracts } from "@example/shared";

const client = new ApiClient({
  baseUrl: "http://localhost:3001/api",
  contracts,
});

After that, client.api mirrors the contract tree:

await client.api.health.get.fetch();
await client.api.todos.list.fetch();
await client.api.todos.create.fetch({ title: "Write docs" });

How requests work

You pass one object containing the fields defined in the contract. The client sorts those fields into params, query, and body for the real HTTP request.

For contracts with no request schema, just call fetch() with no request object:

const health = await client.api.health.get.fetch();

Passing fetch options per request

Each client method also accepts fetch options:

  • for requestless contracts: fetch(options?)
  • for contracts with input: fetch(request, options?)

The client still controls the request method, serialized body, and merged default headers.

Error handling and defaults

You can configure the client once when you create it:

const client = new ApiClient({
  baseUrl: "http://localhost:3001/api",
  contracts,
  timeoutMs: 10_000,
  onHttpError: ({ contract, error }) => {
    console.error("Request failed", contract, error.response.status);
  },
});

// Headers are set via a callback as they are often dynamic and cannot be known at client creation time:
client.setHeaders(async () => {
  const token = await getAuthToken();
  return { Authorization: `Bearer ${token}` };
});

Place in the stack

In a typical app, this package sits between the shared contracts and the React Query adapter:

  1. Import the shared contracts.
  2. Build new ApiClient({ baseUrl, contracts }).
  3. Use client.api directly, or wrap it with @contract-first-api/react-query.

If you are not using React Query, this package is enough on its own for typed API calls.