Skip to content

Commit 10de3e3

Browse files
committed
Add Anchor Browser example for monitoring broadway tickets
1 parent e5dbc10 commit 10de3e3

File tree

6 files changed

+4490
-0
lines changed

6 files changed

+4490
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Trigger.dev Configuration
2+
TRIGGER_API_KEY=tr_dev_your_trigger_api_key_here
3+
4+
# Anchor Browser Configuration
5+
ANCHOR_BROWSER_API_KEY=sk-your_anchor_browser_api_key_here
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
### Project Setup
2+
3+
#### Prerequisites
4+
5+
Before we dive into the code, you'll need:
6+
- Node.js (version 16 or higher)
7+
- A Trigger.dev account - Sign up at https://trigger.dev for their free tier
8+
- Anchor Browser API access - Get your API key from the Anchor Browser dashboard
9+
10+
#### Dependencies installation
11+
12+
Initialize a new Node.js project and install the required packages from Anchor Browser and Trigger.dev:
13+
14+
```
15+
npm init -y
16+
npm install anchorbrowser
17+
npx trigger.dev@latest init -p <project id from trigger.dev>
18+
```
19+
20+
#### Environment configuration
21+
22+
Create a .env file in your project root with the following variables:
23+
24+
```
25+
# Trigger.dev Configuration
26+
TRIGGER_API_KEY=tr_dev_your_trigger_api_key_here
27+
28+
# Anchor Browser Configuration
29+
ANCHOR_BROWSER_API_KEY=sk-your_anchor_browser_api_key_here
30+
```
31+
32+
Make sure to add .env to your .gitignore file to keep your API keys secure:
33+
34+
```
35+
echo ".env" >> .gitignore
36+
```
37+
38+
Since Anchor Browser uses browser automation libraries under the hood, we need to configure Trigger.dev to handle these dependencies properly by excluding them from the build bundle.
39+
40+
Configure Trigger.dev's trigger.config.ts for browser automation dependencies:
41+
42+
```
43+
import { defineConfig } from "@trigger.dev/sdk/v3";
44+
45+
export default defineConfig({
46+
project: "proj_your_project_id_here", // Get from Trigger.dev dashboard
47+
maxDuration: 3600, // 1 hour - plenty of time for web automation
48+
dirs: ["./src/trigger"],
49+
build: {
50+
external: [
51+
"playwright-core",
52+
"playwright",
53+
"chromium-bidi"
54+
]
55+
}
56+
});
57+
```
58+
59+
### Core Monitoring Job
60+
61+
Create a new file within your project subfolder for trigger.dev functions, src/trigger/broadway-monitor.ts. Below is our Broadway ticket monitor task that runs daily at 5pm ET:
62+
63+
```
64+
import { schedules } from "@trigger.dev/sdk";
65+
import Anchorbrowser from 'anchorbrowser';
66+
67+
export const broadwayMonitor = schedules.task({
68+
id: "broadway-ticket-monitor",
69+
cron: "0 21 * * *",
70+
run: async (payload, { ctx }) => {
71+
const client = new Anchorbrowser({
72+
apiKey: process.env.ANCHOR_BROWSER_API_KEY!,
73+
});
74+
75+
let session;
76+
try {
77+
// Create explicit session to get live view URL
78+
session = await client.sessions.create();
79+
console.log(`Session ID: ${session.data.id}`);
80+
console.log(`Live View URL: https://live.anchorbrowser.io?sessionId=${session.data.id}`);
81+
82+
const response = await client.tools.performWebTask({
83+
sessionId: session.data.id,
84+
url: "https://www.tdf.org/discount-ticket-programs/tkts-by-tdf/tkts-live/",
85+
prompt: `Look for the "Broadway Shows" section on this page. Find the show with the absolute lowest starting price available right now and return the show name, current lowest price, and show time. Be very specific about the current price you see. Format as: Show: [name], Price: [exact current price], Time: [time]`
86+
});
87+
88+
console.log("Raw response:", response);
89+
90+
const result = response.data.result?.result || response.data.result || response.data;
91+
92+
if (result && typeof result === 'string' && result.includes('Show:')) {
93+
console.log(`🎭 Best Broadway Deal Found!`);
94+
console.log(result);
95+
96+
return {
97+
success: true,
98+
bestDeal: result,
99+
liveViewUrl: `https://live.anchorbrowser.io?sessionId=${session.data.id}`
100+
};
101+
} else {
102+
console.log("No Broadway deals found today");
103+
return { success: true, message: "No deals found" };
104+
}
105+
106+
} finally {
107+
if (session?.data?.id) {
108+
try {
109+
await client.sessions.delete(session.data.id);
110+
} catch (cleanupError) {
111+
console.warn("Failed to cleanup session:", cleanupError);
112+
}
113+
}
114+
}
115+
},
116+
});
117+
```
118+
119+
Run the Trigger.dev development server from your project root directory to register your new task:
120+
121+
```
122+
npx trigger.dev@latest dev
123+
```

0 commit comments

Comments
 (0)