# @sapphire/fetch

> Tiny wrapper around globalThis.fetch for improved TypeScript and data type support

Latest version **4.0.0** (published 2026-11-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sapphire/fetch
pnpm add @sapphire/fetch
yarn add @sapphire/fetch
bun add @sapphire/fetch
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2026-11-15 |
| First published | 2021-04-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=v24.0.0 |
| Dependencies | 0 |
| Unpacked size | 89.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 192 |
| Author | @sapphire |
| Maintainers | favna, vladfrangu, kyranet |
| Keywords | @sapphire/fetch, bot, typescript, ts, yarn, discord, sapphire, standalone |

## Links

- npm: https://www.npmjs.com/package/@sapphire/fetch
- Repository: https://github.com/sapphiredev/utilities
- Homepage: https://github.com/sapphiredev/utilities/tree/main/packages/fetch
- Issues: https://github.com/sapphiredev/utilities/issues
- npm.io page: https://npm.io/package/@sapphire/fetch

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 4.0.0 (latest) — 2026-11-15
- 3.0.6-next.c1299bac (next) — 2026-03-08
- 4.0.0-pr-935.7da5c8bb (pr-935) — 2025-11-25
- 3.0.3-pr-725.55b1ff7b.0 (pr-725) — 2024-03-17
- 3.0.1-pr-684.9d7e2fce.0 (pr-684) — 2023-12-14
- 3.0.0-pr-601.2f2c308a.0 (pr-601) — 2023-05-28
- 3.0.0-pr-589.aa473f9.0 (pr-589) — 2023-05-04
- 3.0.0-pr-587.e9607661.0 (pr-587) — 2023-05-04
- 3.0.0-pr-584.b444d5b.0 (pr-584) — 2023-05-02
- 2.4.2-pr-575.91d23d87.0 (pr-575) — 2023-04-16
- 2.4.2-pr-567.8638d898.0 (pr-567) — 2023-04-06
- 2.4.2-pr-434.77ef153.0 (pr-434) — 2023-01-06
- 2.1.0-pr-270.b2c4518.0 (pr-270) — 2022-01-26
- 2.0.0-pr-160.676f855.0 (pr-160) — 2021-08-23
- 2.0.0-pr-159.fae7a606.0 (pr-159) — 2021-08-23
- … 985 more at https://npm.io/package/@sapphire/fetch/versions

## README

<div align="center">

![Sapphire Logo](https://raw.githubusercontent.com/sapphiredev/assets/main/banners/SapphireCommunity.png)

# @sapphire/fetch

**Tiny wrapper around Node's global `fetch` for improved TypeScript and data type support**

[![GitHub](https://img.shields.io/github/license/sapphiredev/utilities)](https://github.com/sapphiredev/utilities/blob/main/LICENSE.md)
[![npm bundle size](https://img.shields.io/bundlephobia/min/@sapphire/fetch?logo=webpack&style=flat-square)](https://bundlephobia.com/result?p=@sapphire/fetch)
[![npm](https://img.shields.io/npm/v/@sapphire/fetch?color=crimson&logo=npm&style=flat-square)](https://www.npmjs.com/package/@sapphire/fetch)

</div>

**Table of Contents**

- [Description](#description)
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
    - [`GET`ting JSON data](#getting-json-data)
    - [`GET`ting Buffer data (images, etc.)](#getting-buffer-data-images-etc)
    - [`POST`ing JSON data](#posting-json-data)
- [Buy us some doughnuts](#buy-us-some-doughnuts)
- [Contributors](#contributors)

## Description

Node has a great global `fetch` (powered by [undici]) for making API calls, but because it focuses solely on bringing the [Fetch API][fetch-mdn] to Node.js, it doesn't provide specific error messages and handling for different return types (JSON, Buffer, plain text, etc). This is where `@sapphire/fetch` comes in. The syntax is more restrictive than that of [undici], but that makes it consistent and easier to use in TypeScript.

## Features

- Wraps global `fetch` with helpers for JSON, text, and binary response data
- Exported `enum` for the common return data types
- Provides typed JSON responses and serializes plain-object request bodies
- Throws a descriptive error when a response has a non-success status
- Uses the platform's global Fetch API implementation

## Installation

You can use the following command to install this package, or replace `npm install` with your package manager of choice.

```sh
npm install @sapphire/fetch
```

## TypeScript configuration

When using `@sapphire/fetch` with TypeScript, include `dom` in the `lib` option of your `tsconfig.json`. The package uses Fetch API types such as `RequestInit` and `Response`.

```json
{
	"compilerOptions": {
		"lib": ["ES2022", "dom"]
	}
}
```

## Usage

> [!IMPORTANT]
> When providing a serializable object to the `body` option, `@sapphire/fetch` will automatically call `JSON.stringify` on the object. This means you can pass an object directly to the `body` option without having to call `JSON.stringify` yourself.
> If the body is _not_ serializable (such as a `File`, `Buffer`, or `Blob`), the body will be sent as-is.
> Serializability is calculated based on:
>
> - If the body is `null`
> - If the body's `.constructor` property is `undefined`
> - If the body's `.constructor.name` property is `Object`
> - If the body has a function property named `toJSON`

> [!WARNING]
> Because `@sapphire/fetch` aims to be as close to global fetch as possible, it doesn't support proxy options that a library like undici does. If you want to use a proxy, you should use undici directly.

### `GET`ting JSON data

```typescript
// Import the fetch function
import { fetch, FetchResultTypes } from '@sapphire/fetch';

interface JsonPlaceholderResponse {
	userId: number;
	id: number;
	title: string;
	completed: boolean;
}

// Fetch the data. No need to call `.json()` after making the request!
const data = await fetch<JsonPlaceholderResponse>('https://jsonplaceholder.typicode.com/todos/1', FetchResultTypes.JSON);

// Do something with the data
console.log(data.userId);
```

### `GET`ting Buffer data (images, etc.)

```typescript
// Import the fetch function
import { fetch, FetchResultTypes } from '@sapphire/fetch';

// Fetch the data. No need to call `.buffer()` after making the request!
const sapphireLogo = await fetch('https://github.com/sapphiredev.png', FetchResultTypes.Buffer);

// sapphireLogo is the `Buffer` of the image
console.log(sapphireLogo);
```

### `POST`ing JSON data

```typescript
// Import the fetch function
import { fetch, FetchResultTypes, FetchMethods } from '@sapphire/fetch';

// Post the data. No need to call `.json()` after making the request!
const responseData = await fetch(
	'https://jsonplaceholder.typicode.com/todos',
	{
		method: FetchMethods.Post,
		headers: {
			'Content-Type': 'application/json'
		},
		body: { name: 'John Doe' }
	},
	FetchResultTypes.JSON
);

// Do something with the response data
console.log(responseData);
```

---

## Buy us some doughnuts

Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!

We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.

|   Donate With   |                       Address                       |
| :-------------: | :-------------------------------------------------: |
| Open Collective | [Click Here](https://sapphirejs.dev/opencollective) |
|      Ko-fi      |      [Click Here](https://sapphirejs.dev/kofi)      |
|     Patreon     |    [Click Here](https://sapphirejs.dev/patreon)     |
|     PayPal      |     [Click Here](https://sapphirejs.dev/paypal)     |

## Contributors

Please make sure to read the [Contributing Guide][contributing] before making a pull request.

Thank you to all the people who already contributed to Sapphire!

<a href="https://github.com/sapphiredev/utilities/graphs/contributors">
  <img src="https://contrib.rocks/image?repo=sapphiredev/utilities" />
</a>

[contributing]: https://github.com/sapphiredev/.github/blob/main/.github/CONTRIBUTING.md

<!-- LINKS -->

[fetch-mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
[undici]: https://github.com/nodejs/undici

---
_Source: https://npm.io/package/@sapphire/fetch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
