dkirchhof

payload rest client

by dkirchhof
APIs & SDKs
v?

Overview

Summary generated from the README

A TypeScript client that wraps the Payload REST API and infers types from the generated Payload config types. It exposes typed methods for collections, globals, auth operations and the access endpoint, and supports custom endpoints declared with input and output types. Version 3 targets Payload 3, while older versions serve Payload 2.

Features

  • Typed find, findById, count, create, update and delete methods for every collection
  • Auth methods such as login, logout, me, refresh token and password reset
  • Global get and update methods with locale typing
  • Select, populate and join parameters with inferred result types
  • Custom endpoint definitions with typed params, query and body
  • Options for custom headers, fetch function, cache mode and debug output

Use cases

  • Query Payload content from a separate frontend or Node service with full type safety
  • Call protected routes by logging in and reusing the token in an authorized client
  • Wrap custom Payload endpoints in typed client calls

From the author

A typesafe rest api client for the payload cms.

Installation

npm i payload-rest-client

49

Stars

6

Forks

3

Open Issues

1 years ago

Last Updated

1.0k

Weekly Downloads

v3.0.5

npm Version

56.3 KB

Unpacked Size

0

Dependencies

GitHub stars

490 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 202649
Dec 4, 202649
Dec 3, 202649
Dec 2, 202649
Dec 1, 202649
Nov 30, 202649
Nov 29, 202649
Nov 28, 202649
Nov 27, 202649
Nov 26, 202649
Nov 25, 202649
Nov 24, 202649
Nov 23, 202649
Nov 22, 202649
Nov 21, 202649
Nov 20, 202649
Nov 19, 202649
Nov 18, 202649
Nov 17, 202649
Nov 16, 202649
Nov 15, 202649
Nov 14, 202649
Nov 13, 202649
Nov 12, 202649
Nov 11, 202649
Nov 10, 202649
Nov 9, 202649
Nov 8, 202649
Nov 7, 202649
Nov 6, 202649
Nov 5, 202649
Nov 4, 202649
Nov 3, 202649
Nov 2, 202649
Nov 1, 202649
Oct 31, 202649
Oct 30, 202649
Oct 29, 202649
Oct 28, 202649
Oct 27, 202649
Oct 26, 202649
Oct 25, 202649
Oct 24, 202649
Oct 23, 202649
Oct 22, 202649
Oct 21, 202649
Oct 20, 202649
Oct 19, 202649
Oct 18, 202649
Oct 17, 202649
Oct 16, 202649
Oct 15, 202649
Oct 14, 202649
Oct 13, 202649
Oct 12, 202649
Oct 11, 202649
Oct 10, 202649
Oct 9, 202649
Oct 8, 202649
Oct 7, 202649
Oct 6, 202649
Oct 5, 202649
Oct 4, 202649
Oct 3, 202649
Oct 2, 202649
Oct 1, 202649
Sep 30, 202649
Sep 29, 202649
Sep 28, 202649
Sep 27, 202649
Sep 26, 202649
Sep 25, 202649
Sep 24, 202649
Sep 23, 202649
Sep 22, 202649
Sep 21, 202649
Sep 20, 202649
Sep 19, 202649
Sep 18, 202649
Sep 17, 202649
Sep 16, 202649
Sep 15, 202649
Sep 14, 202649
Sep 13, 202649
Sep 12, 202649
Sep 11, 202649
Sep 10, 202649
Sep 9, 202649
Sep 8, 202649
Sep 7, 202649
Sep 6, 202649
npm weekly downloads

1.1K+601 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 20261,076
Dec 4, 20261,124
Dec 3, 20261,081
Dec 2, 20261,062
Dec 1, 20261,016
Nov 30, 2026895
Nov 29, 2026925
Nov 28, 2026789
Nov 27, 2026763
Nov 26, 20261,001
Nov 25, 20261,184
Nov 24, 20261,368
Nov 23, 20261,575
Nov 22, 20261,478
Nov 21, 20261,564
Nov 20, 20261,466
Nov 19, 20261,217
Nov 18, 20261,146
Nov 17, 2026866
Nov 16, 2026773
Nov 15, 2026746
Nov 14, 2026633
Nov 13, 2026654
Nov 12, 2026643
Nov 11, 2026542
Nov 10, 2026570
Nov 9, 2026468
Nov 8, 2026393
Nov 7, 2026379
Nov 6, 2026338
Nov 5, 2026309
Nov 4, 2026300
Nov 3, 2026260
Nov 2, 2026311
Nov 1, 2026507
Oct 31, 2026530
Oct 30, 2026515
Oct 29, 2026450
Oct 28, 2026403
Oct 27, 2026405
Oct 26, 2026355
Oct 25, 2026138
Oct 24, 2026121
Oct 23, 202691
Oct 22, 2026146
Oct 21, 2026233
Oct 20, 2026255
Oct 19, 2026268
Oct 18, 2026382
Oct 17, 2026386
Oct 16, 2026410
Oct 15, 2026405
Oct 14, 2026404
Oct 13, 2026424
Oct 12, 2026427
Oct 11, 2026304
Oct 10, 2026289
Oct 9, 2026307
Oct 8, 2026787
Oct 7, 2026782
Oct 6, 2026741
Oct 5, 2026715
Oct 4, 2026882
Oct 3, 2026893
Oct 2, 20261,056
Oct 1, 2026562
Sep 30, 2026519
Sep 29, 2026558
Sep 28, 2026583
Sep 27, 2026423
Sep 26, 2026409
Sep 25, 2026254
Sep 24, 2026398
Sep 23, 2026405
Sep 22, 2026387
Sep 21, 2026391
Sep 20, 2026382
Sep 19, 2026408
Sep 18, 2026487
Sep 17, 2026351
Sep 16, 2026347
Sep 15, 2026365
Sep 14, 2026357
Sep 13, 2026428
Sep 12, 2026441
Sep 11, 2026377
Sep 10, 2026439
Sep 9, 2026500
Sep 8, 2026475
Sep 7, 2026528
Sep 6, 2026475

Each point is npm's rolling 7-day download count.

Health Score

43/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/704946675-root.svg)](https://payloaddirectory.dev/plugins/704946675-root)
HTML
<a href="https://payloaddirectory.dev/plugins/704946675-root"><img src="https://payloaddirectory.dev/badge/704946675-root.svg" alt="Payload Directory"></a>
Updated Feb 19, 2025Created Oct 14, 2023Published Feb 19, 2025

README

# payload-rest-client

A typesafe rest api client for the [payload cms](https://payloadcms.com).

## Quick Start 

1. Assume you have a users (auth enabled) and a posts collection with following fields:

```ts
interface User {
    id: string;
    email: string;
    name: string;
    password: string;
    createdAt: string;
    updatedAt: string;
}

interface Post {
    id: string;
    title: string;
    content: string;
    createdAt: string;
    updatedAt: string;
}
```

2. Create the client:
Full README
# payload-rest-client

A typesafe rest api client for the [payload cms](https://payloadcms.com).

## Quick Start 

1. Assume you have a users (auth enabled) and a posts collection with following fields:

```ts
interface User {
    id: string;
    email: string;
    name: string;
    password: string;
    createdAt: string;
    updatedAt: string;
}

interface Post {
    id: string;
    title: string;
    content: string;
    createdAt: string;
    updatedAt: string;
}
```

2. Create the client:

```ts
import { createClient } from "payload-rest-client";
import { Config } from "./payload-types"; // auto generated types from payload

type Locales = "de" | "en";

const client = createClient<Config, Locales>({
    apiUrl: "http://localhost:4000/api",
});
```

3. Now you can use all available queries for all collections and globals in a typesafe way:

```ts
// if you wan't to use protected routes, use login api...
const loginResponse = await client.collections.users.login({
    email: process.env.PAYLOAD_API_EMAIL,
    password: process.env.PAYLOAD_API_PASSWORD,
});

// ...and create another client with authorization header
const protectedClient = createClient<Config, Locales>({
    apiUrl: "http://localhost:4000/api",
    headers: {
        "Authorization": `Bearer ${loginResponse.token}`,
    },
});

const posts = await protectedClient.collections.posts.find({
    sort: "title", // only top level keys (optionally prefixed with "-") of Post allowed
    locale: "de", // only defined locales allowed
    limit: 10,
    page: 2,
});

console.log(posts); // type of posts is FindResult<Post> 
```

## Custom Endpoints

1. Define input and output types for alle custom endpoints:

```ts
import { CustomEndpoint } from "payload-rest-client";

/**
 * shape of generic CustomEndpoint type
 *
 * type Input = {
 *     params?: Record<string, string>;
 *     query?: Record<string, any>;
 *     body?: any;
 * };
 *
 * type Output = any;
 *
 * type CustomEndpoint<Input, Output>;
 */

type CustomEndpoints = {
    greet: CustomEndpoint<{
        params: { name: string };
        query: { locale: Locales };
    }, string>,
};
```

2. Add it to `createClient` function:

```diff
-const client = createClient<Config, Locales>({
-    apiUrl: "http://localhost:4000/api",
-});
+const client = createClient<Config, Locales, CustomEndpoints>({
+    apiUrl: "http://localhost:4000/api",
+    customEndpoints: {
+        greet: { method: "GET", path: params => `hello/${params.name}` },
+    },
+});
```

3. Call custom endpoints like this:

```ts
const greeting = await client.custom.greet({
    params: { name: "John Doe" },
    query: { locale: "en" },
});
```

## API

[Full documentation of the rest api](https://payloadcms.com/docs/rest-api/overview)

### Client options

- apiUrl: string;
- cache?: RequestCache;
- headers?: HeadersInit;
- debug?: boolean;
- getAdditionalFetchOptions?: (params: GetAdditionalFetchOptionsParams) => any;
- customFetchFn? (input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
- customEndpoints?: Record<String, CustomEndpointFactory>;

### Collections

- find: (params?: FindParams<T, LOCALES>) => Promise<FindResult<T>>;
- findById: (params: FindByIdParams<LOCALES>) => Promise<T>;
- count: (params: CountParams<T>) => Promise<CountResult>;
- create: (params: CreateParams<T, LOCALES>) => Promise<CreateResult<T>>;
- createDraft: (params: CreateDraftParams<T, LOCALES>) => Promise<CreateDraftResult<T>>;
- update: (params: UpdateParams<T, LOCALES>) => Promise<UpdateResult<T>>;
- updateById: (params: UpdateByIdParams<T, LOCALES>) => Promise<UpdateByIdResult<T>>;
- delete: (params?: DeleteParams<T, LOCALES>) => Promise<DeleteResult<T>>;
- deleteById: (params: DeleteByIdParams<LOCALES>) => Promise<T>;

### Collections with auth enabled (additional to above)

- login: (params: LoginParams) => Promise<LoginResult<T>>;
- logout: (params: LogoutParams) => Promise<LogoutResult>;
- unlock: (params: UnlockParams) => Promise<UnlockResult>;
- refresh-token: (params: RefreshTokenParams) => Promise<RefreshTokenResult>;
- me: (params: MeParams) => Promise<MeResult<T>>;
- forgot-password: (params: ForgotPasswordParams) => Promise<ForgotPasswordResult>;
- reset-password: (params: ResetPasswordParams) => Promise<ResetPasswordResult<T>>;

### Globals

- get: (params?: BaseParams<LOCALES>) => Promise<T>;
- update: (params: UpdateGlobalParams<T, LOCALES>) => Promise<T>;

### Others

- access: () => Promise<AccessResult>;

## Changelog

### v 3.0.5

- Infer id params (`string` or `number`) from type.

### v 3.0.4

- Added custom endpoints

### v 3.0.3

- Added option to use custom fetch function

### v 3.0.2

- Export error types
- Added access api

### v 3.0.1

- Better type inference for joins

### v 3.0.0

- Payload 3 (for Payload 2 use older versions)
- Added `select`, `populate` and `join` params
- Added `count` api

Similar plugins