Astro
How to manually setup Trigger.dev in your Astro project
Install Required Packages
To begin, install the necessary packages in your Astro project directory. You can choose one of the following package managers:
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 with tr_dev_
).
Adding Environment Variables
Create a .env
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.
Configuring the Trigger Client
Create a file at <root>/trigger.ts
or <root>/src/trigger.ts
, depending on if your project uses a src
directory, where <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-astro-app" with an appropriate identifier for your project.
Update the astro.config file to enable SSR (Server Side Rendering)
- You need to enable SSR to use API endpoints (which are required by Trigger.dev).
To learn more about SSR, head over to the Astro docs on SSR.
Creating an Example Job
- Create a folder named
jobs
alongside yourpages
directory - Inside the
jobs
folder, add two files namedexample.ts
andindex.ts
.
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
- Create a new file named
trigger.ts
within thepages/api/
directory. - Add the following code to
trigger.ts
:
Adding Configuration to 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-astro-app" with the appropriate identifier you used during the step for creating the TriggerClient
.
Additonal Job Definitions
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
, you can export other job files like this:
Running
Run your Astro app
Run your Astro app locally, like you normally would. For example:
Run the CLI 'dev' command
In a separate terminal window or tab run:
Astro by default runs on port 4321.
You can optionally pass the hostname if you're not running on localhost by adding
--hostname <host>
. Example, in case your Astro app is running on 0.0.0.0: --hostname 0.0.0.0
.
Next Steps
You should now see your example job in the Trigger.dev dashboard. You can now create additional jobs and use the Trigger.dev dashboard to test them.
Was this page helpful?