Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Using Payload outside Next.js

Payload can be used completely outside of Next.js which is helpful in cases like running scripts, using Payload in a separate backend service, or using Payload's Local API to fetch your data directly from your database in other frontend frameworks like SvelteKit, Remix, Nuxt, and similar.

Importing the Payload Config outside of Next.js

Payload provides a convenient way to run standalone scripts, which can be useful for tasks like seeding your database or performing one-off operations.

In standalone scripts, you can simply import the Payload Config and use it right away. If you need an initialized copy of Payload, you can then use the getPayload function. This can be useful for tasks like seeding your database or performing other one-off operations.

1
import { getPayload } from 'payload'
2
import config from '@payload-config'
3
4
const seed = async () => {
5
// Get a local copy of Payload by passing your config
6
const payload = await getPayload({ config })
7
8
const user = await payload.create({
9
collection: 'users',
10
data: {
11
email: 'dev@payloadcms.com',
12
password: 'some-password'
13
}
14
})
15
16
const page = await payload.create({
17
collection: 'pages',
18
data: {
19
title: 'My Homepage',
20
// other data to seed here
21
}
22
})
23
}
24
25
// Call the function here to run your seed script
26
await seed()

You can then execute the script using payload run. Example: if you placed this standalone script in src/seed.ts, you would execute it like this:

1
payload run src/seed.ts

The payload run command does two things for you:

  1. It loads the environment variables the same way Next.js loads them, eliminating the need for additional dependencies like dotenv. The usage of dotenv is not recommended, as Next.js loads environment variables differently. By using payload run, you ensure consistent environment variable handling across your Payload and Next.js setup.
  2. It initializes tsx, allowing direct execution of TypeScript files manually installing tools like tsx or ts-node.

Troubleshooting

If you encounter import-related errors, you have 2 options:

Option 1: enable swc mode by appending --use-swc to the payload command:

Example:

1
payload run src/seed.ts --use-swc

Note: Install @swc-node/register in your project first. While swc mode is faster than the default tsx mode, it might break for some imports.

Option 2: use an alternative runtime like bun

While we do not guarantee support for alternative runtimes, you are free to use them and disable payloads own transpilation by appending the --disable-transpilation flag to the payload command:

1
bunx --bun payload run src/seed.ts --disable-transpile

You will need to have bun installed on your system for this to work.

Next

REST API