Back to APIs

Manage payments, expenses, teams and more with Brex.

Using the Brex API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Brex background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Example code using Brex

Below is a working code example of how you can use Brex with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
2
import z from "zod";
3
4
// Docs: https://developer.brex.com/openapi/team_api/#tag/Titles
5
// API: https://developer.brex.com/openapi/team_api/#operation/createTitle
6
const endpointURL = `${process.env.BREX_BASE_URL}/titles`; // Replace with the Other Brex API endpoint
7
8
// Create tokens at https://developer.brex.com/docs/quickstart/#1-generate-your-user-token
9
// Scopes: Titles: read, write
10
// Create request options
11
const requestOptions: RequestInit = {
12
method: "POST",
13
headers: {
14
Authorization: `Bearer ${process.env.BREX_API_KEY}`,
15
"Content-Type": "application/json",
16
},
17
};
18
19
client.defineJob({
20
id: "brex-create-title",
21
name: "Brex Create Title",
22
version: "1.0.0",
23
trigger: eventTrigger({
24
name: "Brex Create Title",
25
schema: z.object({
26
// Name of the title. You can see the all titles in the teams section.
27
// https://dashboard.brex.com/p/team/titles
28
name: z.string(),
29
}),
30
}),
31
run: async (payload, io, ctx) => {
32
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
33
await io.runTask(
34
"Brex Create Title",
35
async () => {
36
// Make a request to the Brex API using the fetch API
37
const response = await fetch(endpointURL, {
38
...requestOptions,
39
body: JSON.stringify(payload),
40
});
41
42
// Return the response body
43
return await response.json();
44
},
45
46
// Add metadata to improve how the task displays in the logs
47
{ name: "Brex Create Title", icon: "brex" }
48
);
49
},
50
});