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.
If you're working 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, getPayload
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.
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. |
select | Specify select to control which fields to include to the result. |
populate | Specify populate to control which fields to include to the result from populated 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. |
disableTransaction | When set to true , a database transactions will not be initialized. |
There are more options available on an operation by operation basis outlined below.
Transactions
When your database uses transactions you need to thread req through to all local operations. Postgres uses transactions and MongoDB uses transactions when you are using replica sets. Passing req without transactions is still recommended.
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:
Auth
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: