Node.js Setup
Get started with Schedo in your Node.js application in minutes.
Installation
Install the Schedo Node.js client using npm, yarn, or pnpm:
bash
1npm install @schedo/node
bash
1yarn add @schedo/node
bash
1pnpm add @schedo/node
Basic Usage
Import and initialize the Schedo client with your API key:
typescript
1import { Schedo } from class="text-amber-300">'@schedo/node';
2
3const schedo = new Schedo({
4 apiKey: process.env.SCHEDO_API_KEY
5});
Scheduling Jobs
Schedule your first job with a cron expression:
typescript
1class=class="text-amber-300">"text-gray-500">// Schedule a job to run every day at 2 AM
2await schedo.jobs.create({
3 name: class="text-amber-300">'daily-cleanup',
4 schedule: class="text-amber-300">'0 2 * * *',
5 async handler() {
6 class=class="text-amber-300">"text-gray-500">// Your job logic here
7 await cleanupOldRecords();
8 }
9});
10
11class=class="text-amber-300">"text-gray-500">// Schedule a job to run every 5 minutes
12await schedo.jobs.create({
13 name: class="text-amber-300">'check-metrics',
14 schedule: class="text-amber-300">'*/5 * * * *',
15 async handler() {
16 await checkSystemMetrics();
17 }
18});
Error Handling
Handle job failures and retries:
typescript
1await schedo.jobs.create({
2 name: class="text-amber-300">'process-orders',
3 schedule: class="text-amber-300">'0 */2 * * *',
4 maxRetries: 3,
5 async handler(ctx) {
6 try {
7 await processOrders();
8 } catch (error) {
9 ctx.logger.error(class="text-amber-300">'Failed to process orders:', error);
10 throw error; class=class="text-amber-300">"text-gray-500">// Schedo will handle retries
11 }
12 }
13});
Documentation
For more detailed information and advanced features, please refer to our documentation: