@mrzr/api-client

Introduction

@mrzr/api-client is a zero-dependency TypeScript fetch client that handles browser auth for you: token refresh, Web Worker token isolation and cross-tab sync.

@mrzr/api-client is a small HTTP client built on fetch. You make requests; it handles the tokens.

import { createClient } from "@mrzr/api-client";

export const api = createClient({ baseUrl: "https://api.example.com" });

await api.login({ email, password });               // tokens are stored for you
const { data } = await api.get<User[]>("/users");   // Authorization is attached for you

What it does for you

When…The client…
The access token expires while 20 requests are in flightSends one refresh request, then retries all 20
An injected script looks for the tokenCan't find it: requests run in a Web Worker and the token stays there
The user logs out in one tabLogs out every other tab too
The token is about to expireRefreshes it before the next request fails
The user leaves the pageCan cancel the requests that page started

No runtime dependencies. Works in browsers, Node 20+, Deno, Bun and Cloudflare Workers.

Is it for you?

Use it if you have a browser app that logs users in and calls your own API, and you're tired of writing refresh-token interceptors.

If you only need a thin fetch wrapper with retries, ky is a better fit. This client has no retry option.

Next

  • Quick start: set it up in five minutes
  • Live demo: watch the refresh and cancellation happen in your browser

On this page