You are currently viewing documentation for version 2 of Payload.
Local API
The Payload Local API gives you the ability to execute the same operations that are available through REST and GraphQL
within Node, directly on your server. Here, you don't need to deal with server latency or network speed whatsoever and
can interact directly with your database.
Tip:
The Local API is incredibly powerful when used with server-side rendering app frameworks like
NextJS. With other headless CMS, you need to request your data from third-party servers which can
add significant loading time to your server-rendered pages. With Payload, you don't have to leave
your server to gather the data you need. It can be incredibly fast and is definitely a game
changer.
Here are some common examples of how you can use the Local API:
Seeding data via Node seed scripts that you write and maintain
Opening custom Express routes which feature additional functionality but still rely on Payload
You can import or require payload into your own files after it's been initialized, but you need to make sure that
your import / require statements come after you call payload.init()—otherwise Payload won't have been
initialized yet. That might be obvious. To us, it's usually not.
Specify a fallback locale to use for any returned documents.
overrideAccess
Skip access control. By default, this property is set to true within all Local API operations.
user
If you set overrideAccess to false, you can pass a user to use against the access control checks.
showHiddenFields
Opt-in to receiving hidden fields. By default, they are hidden from returned documents in accordance to your config.
pagination
Set to false to return all documents and avoid querying for document counts.
context
Context, which will then be passed to context and req.context, which can be read by hooks. Useful if you want to pass additional information to the hooks which shouldn't be necessarily part of the document, for example a triggerBeforeChange option which can be read by the BeforeChange hook to determine if it should run or not.
There are more options available on an operation by operation basis outlined below.
Note:
By default, all access control checks are disabled in the Local API, but you can re-enable them if
you'd like, as well as pass a specific user to run the operation with.
There is a known issue when using the Local API with Next.js version 13.4.13 and higher. Next.js executes within a
separate child process, and Payload has not been initalized yet in these instances. That means that unless you
explicitly initialize Payload within your operation, it will not be running and return no data / an empty object.
As a workaround, we recommend leveraging the following pattern to determine and ensure Payload is initalized:
1
importdotenvfrom'dotenv'
2
importpathfrom'path'
3
import type {Payload}from'payload'
4
importpayloadfrom'payload'
5
import type {InitOptions}from'payload/config'
6
import{ seed as seedData }from'./seed'
7
8
dotenv.config({
9
path: path.resolve(__dirname,'../.env'),
10
})
11
12
let cached =(global as any).payload
13
14
if(!cached){
15
cached =(global as any).payload={client:null,promise:null}