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.

Tasks

You can register Tasks on the Payload config, and then create Jobs or Workflows that use them. Think of Tasks like tidy, isolated "functions that do one specific thing".

Payload Tasks can be configured to automatically retried if they fail, which makes them valuable for "durable" workflows like AI applications where LLMs can return non-deterministic results, and might need to be retried.

Tasks can either be defined within the jobs.tasks array in your payload config, or they can be defined inline within a workflow.

Defining tasks in the config

Simply add a task to the jobs.tasks array in your Payload config. A task consists of the following fields:

OptionDescription
slugDefine a slug-based name for this job. This slug needs to be unique among both tasks and workflows.
handlerThe function that should be responsible for running the job. You can either pass a string-based path to the job function file, or the job function itself. If you are using large dependencies within your job, you might prefer to pass the string path because that will avoid bundling large dependencies in your Next.js app. Passing a string path is an advanced feature that may require a sophisticated build pipeline in order to work.
inputSchemaDefine the input field schema - payload will generate a type for this schema.
interfaceNameYou can use interfaceName to change the name of the interface that is generated for this task. By default, this is "Task" + the capitalized task slug.
outputSchemaDefine the output field schema - payload will generate a type for this schema.
labelDefine a human-friendly label for this task.
onFailFunction to be executed if the task fails.
onSuccessFunction to be executed if the task succeeds.
retriesSpecify the number of times that this step should be retried if it fails. If this is undefined, the task will either inherit the retries from the workflow or have no retries. If this is 0, the task will not be retried. By default, this is undefined.

The logic for the Task is defined in the handler - which can be defined as a function, or a path to a function. The handler will run once a worker picks picks up a Job that includes this task.

It should return an object with an output key, which should contain the output of the task as you've defined.

Example:

1
export default buildConfig({
2
// ...
3
jobs: {
4
tasks: [
5
{
6
// Configure this task to automatically retry
7
// up to two times
8
retries: 2,
9
10
// This is a unique identifier for the task
11
12
slug: 'createPost',
13
14
// These are the arguments that your Task will accept
15
inputSchema: [
16
{
17
name: 'title',
18
type: 'text',
19
required: true,
20
},
21
],
22
23
// These are the properties that the function should output
24
outputSchema: [
25
{
26
name: 'postID',
27
type: 'text',
28
required: true,
29
},
30
],
31
32
// This is the function that is run when the task is invoked
33
handler: async ({ input, job, req }) => {
34
const newPost = await req.payload.create({
35
collection: 'post',
36
req,
37
data: {
38
title: input.title,
39
},
40
})
41
return {
42
output: {
43
postID: newPost.id,
44
},
45
}
46
},
47
} as TaskConfig<'createPost'>,
48
]
49
}
50
})

In addition to defining handlers as functions directly provided to your Payload config, you can also pass an absolute path to where the handler is defined. If your task has large dependencies, and you are planning on executing your jobs in a separate process that has access to the filesystem, this could be a handy way to make sure that your Payload + Next.js app remains quick to compile and has minimal dependencies.

Keep in mind that this is an advanced feature that may require a sophisticated build pipeline, especially when using it in production or within Next.js, e.g. by calling opening the /api/payload-jobs/run endpoint. You will have to transpile the handler files separately and ensure they are available in the same location when the job is run. If you're using an endpoint to execute your jobs, it's recommended to define your handlers as functions directly in your Payload Config, or use import paths handlers outside of Next.js.

In general, this is an advanced use case. Here's how this would look:

payload.config.ts:

1
import { fileURLToPath } from 'node:url'
2
import path from 'path'
3
4
const filename = fileURLToPath(import.meta.url)
5
const dirname = path.dirname(filename)
6
7
export default buildConfig({
8
jobs: {
9
tasks: [
10
{
11
// ...
12
// The #createPostHandler is a named export within the `createPost.ts` file
13
handler: path.resolve(dirname, 'src/tasks/createPost.ts') + '#createPostHandler',
14
}
15
]
16
}
17
})

Then, the createPost file itself:

src/tasks/createPost.ts:

1
import type { TaskHandler } from 'payload'
2
3
export const createPostHandler: TaskHandler<'createPost'> = async ({ input, job, req }) => {
4
const newPost = await req.payload.create({
5
collection: 'post',
6
req,
7
data: {
8
title: input.title,
9
},
10
})
11
return {
12
output: {
13
postID: newPost.id,
14
},
15
}
16
}
Next

Workflows