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:
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 Trigger.dev works with either the Pages or App Router configuration.
create-next-app command in your terminal:Installing Required Packages
To begin, install the necessary packages in your Next.js project directory. You can choose one of the following package managers:Ensure that you execute this command within a Next.js project.
Obtaining the Development API Key
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 withtr_dev_).
Adding Environment Variables
Create a.env.local file at the root of your project and include your Trigger API key and URL like this:
ENTER_YOUR_DEVELOPMENT_API_KEY_HERE with the actual API key obtained from the previous step.
Configuring the Trigger Client
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:
src/trigger.(ts/js)
Creating the API Route
To establish an API route for interacting with Trigger.dev, follow these steps based on your project’s file type and structure- App Directory
- Pages Directory
- Create a new file named
route.(ts/js)within theapp/api/trigger/directory. - Add the following code to
route.(ts/js):
Creating the Example Job
- Create a folder named
Jobsalongside yourapporpagesdirectory - Inside the
Jobsfolder, add two files namedexample.(ts/js)andindex.(ts/js).
Additonal Job Definitions
You can define more job definitions by creating additional files in theJobs folder and exporting them in index file.
For example, in index.(ts/js), you can export other job files like this:
Adding Configuration to package.json
Inside the package.json file, add the following configuration under the root object:
package.json file might look something like this:
Running
Run your Next.js app
Run your Next.js app locally, like you normally would. For example:Run the CLI ‘dev’ command
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 endYou 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.
