How to manually setup Trigger.dev in your Next.js project
Don't have a Next.js project yet to add Trigger.dev to? No problem, you can complete the Manual Setup using a blank Next.js project:
Create a blank project by running the create-next-app
command in your terminal:
Trigger.dev works with either the Pages or App Router configuration.
To begin, install the necessary packages in your Next.js project directory. You can choose one of the following package managers:
To locate your development API key, login to the Trigger.dev
dashboard and select the Project you want to
connect to. Then click on the Environments & API Keys tab in the left menu.
You can copy your development API Key from the field at the top of this page.
(Your development key will start with tr_dev_
).
Create a .env.local
file at the root of your project and include your Trigger API key and URL like this:
Replace ENTER_YOUR_DEVELOPMENT_API_KEY_HERE
with the actual API key obtained from the previous step.
Create a file at <root>/src/trigger.ts
or <root>/trigger.ts
depending on whether you’re using the src
directory or not. <root>
represents the root directory of your project.
Next, add the following code to the file which creates and exports a new TriggerClient
:
Replace “my-app” with an appropriate identifier for your project.
To establish an API route for interacting with Trigger.dev, follow these steps based on your project’s file type and structure
route.(ts/js)
within the app/api/trigger/
directory.route.(ts/js)
:route.(ts/js)
within the app/api/trigger/
directory.route.(ts/js)
:trigger.(ts/js)
within the pages/api/
directory.trigger.(ts/js)
:In the code blocks, replace ”@/trigger” with the appropriate path to your Trigger Client configuration file, and adjust the path to the Jobs folder accordingly. Make sure to provide the correct paths if your project isn’t utilizing the Next.js alias feature.
Jobs
alongside your app
or pages
directoryJobs
folder, add two files named example.(ts/js)
and index.(ts/js)
.You can define more job definitions by creating additional files in the Jobs
folder and exporting them in index
file.
For example, in index.(ts/js)
, you can export other job files like this:
package.json
Inside the package.json
file, add the following configuration under the root object:
Your package.json
file might look something like this:
Replace “my-app” with the appropriate identifier you used during the step for creating the Trigger Client.
Run your Next.js app locally, like you normally would. For example:
In a separate terminal window or tab run:
You can optionally pass the port if you’re not running on 3000 by adding
--port 3001
to the end
You can optionally pass the hostname if you’re not running on localhost by adding
--hostname <host>
. Example, in case your Remix is running on 0.0.0.0: --hostname 0.0.0.0
.
If your existing Next.js project utilizes middleware and you encounter any issues, such as potential conflicts with Trigger.dev, it’s recommended to refer to the troubleshooting guide at Middleware for assistance. This guide can help you address any concerns related to middleware conflicts and ensure the smooth functioning of your project with Trigger.dev.
How to manually setup Trigger.dev in your Next.js project
Don't have a Next.js project yet to add Trigger.dev to? No problem, you can complete the Manual Setup using a blank Next.js project:
Create a blank project by running the create-next-app
command in your terminal:
Trigger.dev works with either the Pages or App Router configuration.
To begin, install the necessary packages in your Next.js project directory. You can choose one of the following package managers:
To locate your development API key, login to the Trigger.dev
dashboard and select the Project you want to
connect to. Then click on the Environments & API Keys tab in the left menu.
You can copy your development API Key from the field at the top of this page.
(Your development key will start with tr_dev_
).
Create a .env.local
file at the root of your project and include your Trigger API key and URL like this:
Replace ENTER_YOUR_DEVELOPMENT_API_KEY_HERE
with the actual API key obtained from the previous step.
Create a file at <root>/src/trigger.ts
or <root>/trigger.ts
depending on whether you’re using the src
directory or not. <root>
represents the root directory of your project.
Next, add the following code to the file which creates and exports a new TriggerClient
:
Replace “my-app” with an appropriate identifier for your project.
To establish an API route for interacting with Trigger.dev, follow these steps based on your project’s file type and structure
route.(ts/js)
within the app/api/trigger/
directory.route.(ts/js)
:route.(ts/js)
within the app/api/trigger/
directory.route.(ts/js)
:trigger.(ts/js)
within the pages/api/
directory.trigger.(ts/js)
:In the code blocks, replace ”@/trigger” with the appropriate path to your Trigger Client configuration file, and adjust the path to the Jobs folder accordingly. Make sure to provide the correct paths if your project isn’t utilizing the Next.js alias feature.
Jobs
alongside your app
or pages
directoryJobs
folder, add two files named example.(ts/js)
and index.(ts/js)
.You can define more job definitions by creating additional files in the Jobs
folder and exporting them in index
file.
For example, in index.(ts/js)
, you can export other job files like this:
package.json
Inside the package.json
file, add the following configuration under the root object:
Your package.json
file might look something like this:
Replace “my-app” with the appropriate identifier you used during the step for creating the Trigger Client.
Run your Next.js app locally, like you normally would. For example:
In a separate terminal window or tab run:
You can optionally pass the port if you’re not running on 3000 by adding
--port 3001
to the end
You can optionally pass the hostname if you’re not running on localhost by adding
--hostname <host>
. Example, in case your Remix is running on 0.0.0.0: --hostname 0.0.0.0
.
If your existing Next.js project utilizes middleware and you encounter any issues, such as potential conflicts with Trigger.dev, it’s recommended to refer to the troubleshooting guide at Middleware for assistance. This guide can help you address any concerns related to middleware conflicts and ensure the smooth functioning of your project with Trigger.dev.