REST API in payload CMS

A fully functional REST API is automatically generated from your Collection and Global configs.

All CRUD operations are exposed as follows:

Auth Operations

Auth enabled collections are also given the following endpoints:

Global:

example:

import { CollectionConfig } from ‘payload/types’

2

3// a collection of ‘orders’ with an additional route for tracking details, reachable at /api/orders/:id/tracking

4export const Orders: CollectionConfig = {

5 slug: ‘orders’,

6 fields: [

7 /* … */

8 ],

9 endpoints: [

10 {

11 path: ‘/:id/tracking’,

12 method: ‘get’,

13 handler: async (req, res, next) => {

14 const tracking = await getTrackingInfo(req.params.id)

15 if (tracking) {

16 res.status(200).send({ tracking })

17 } else {

18 res.status(404).send({ error: ‘not found’ })

19 }

20 },

21 },

22 ],

23}

Leave a comment

Your email address will not be published. Required fields are marked *