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.
Here are some common examples of how you can use the Local API:
- Fetching Payload data within React Server Components
- Seeding data via Node seed scripts that you write and maintain
- Opening custom Next.js route handlers which feature additional functionality but still rely on Payload
- Within Access Control and Hooks
Accessing Payload
You can gain access to the currently running payload
object via two ways:
Accessing from args or req
In most places within Payload itself, you can access payload
directly from the arguments of Hooks, Access Control, Validation functions, and similar. This is the simplest way to access Payload in most cases. Most config functions take the req
(Request) object, which has Payload bound to it (req.payload
).
Example:
Importing it
If you want to import Payload in places where you don't have the option to access it from function arguments or req
, you can import it and initialize it.
There are two places to import Payload:
Option 1 - using HMR, within Next.js
You should import Payload using the first option (getPayloadHMR
) if you are using Payload inside of Next.js (like route handlers, server components, and similar.)
This way, in Next.js development mode, Payload will work with Hot Module Replacement (HMR), and as you make changes to your Payload Config, your usage of Payload will always be in sync with your changes. In production, getPayloadHMR
simply disables all HMR functionality so you don't need to write your code any differently. We handle optimization for you in production mode.
If you are accessing Payload via function arguments or req.payload
, HMR is automatically supported if you are using it within Next.js.
Option 2 - outside of Next.js
If you are using Payload outside of Next.js, for example in standalone scripts or in other frameworks, you can import Payload with no HMR functionality. Instead of using getPayloadHMR
, you can use getPayload
.
Both options function in exactly the same way outside of one having HMR support and the other not. For more information about using Payload outside of Next.js, click here.
Local options available
You can specify more options within the Local API vs. REST or GraphQL due to the server-only context that they are executed in.
Local Option | Description |
---|---|
collection | Required for Collection operations. Specifies the Collection slug to operate against. |
data | The data to use within the operation. Required for create , update . |
depth | Control auto-population of nested relationship and upload fields. |
locale | Specify locale for any returned documents. |
fallbackLocale | 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. |
overrideLock | By default, document locks are ignored (true ). Set to false to enforce locks and prevent operations when a document is locked by another user. More details. |
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. |
disableErrors | When set to true , errors will not be thrown. Instead, the findByID operation will return null , and the find operation will return an empty documents array. |
There are more options available on an operation by operation basis outlined below.
Collections
The following Collection operations are available through the Local API:
Create
Find
Find by ID
Count
Update by ID
Update Many
Delete
Delete Many
Auth Operations
If a collection has Authentication
enabled, additional Local API operations will be
available:
Login
Forgot Password
Reset Password
Unlock
Verify
Globals
The following Global operations are available through the Local API:
Find
Update
TypeScript
Local API calls will automatically infer your generated types.
Here is an example of usage: