Create, debug, or modify QStash workflows for data updates and social media posting in the API service. Use when adding new automated jobs, fixing workflow errors, or updating scheduling logic.
This skill helps you work with QStash-based workflows in apps/api/src/lib/workflows/.
The project uses QStash workflows with the following structure:
apps/api/src/lib/workflows/
โโโ cars/ # Car registration data workflows
โ โโโ update.ts # Scheduled car data updates
โโโ coe/ # COE bidding data workflows
โ โโโ update.ts # Scheduled COE data updates
โโโ social/ # Social media posting workflows
โโโ discord.ts
โโโ linkedin.ts
โโโ telegram.ts
โโโ twitter.ts
Workflows are defined using QStash SDK:
import { serve } from "@upstash/workflow";
export const POST = serve(async (context) => {
// Step 1: Fetch data
await context.run("fetch-data", async () => {
// Fetching logic
});
// Step 2: Process data
const processed = await context.run("process-data", async () => {
// Processing logic
});
// Step 3: Store results
await context.run("store-results", async () => {
// Storage logic
});
});
Workflows are triggered via cron schedules configured in:
infra/)Always include comprehensive error handling:
await context.run("step-name", async () => {
try {
// Logic here
} catch (error) {
console.error("Step failed:", error);
// Log to monitoring service
throw error; // Re-throw for workflow retry
}
});
context.run()apps/api/src/routes/Workflows typically need:
DATABASE_URL - PostgreSQL connectionUPSTASH_REDIS_REST_URL / UPSTASH_REDIS_REST_TOKEN - RedisQSTASH_TOKEN - QStash authenticationRun workflow tests:
pnpm -F @sgcarstrends/api test -- src/lib/workflows
Test individual workflow locally:
# Start dev server
pnpm dev
# Trigger workflow via HTTP
curl -X POST http://localhost:3000/api/workflows/cars/update
apps/api/src/routes/workflows.ts - Workflow route handlersapps/api/src/config/qstash.ts - QStash configurationapps/api/CLAUDE.md - API service documentation