Using Schedules — Agentuity Documentation

Using Schedules

Create and manage cron-based scheduled jobs with ctx.schedule

Create platform-managed cron jobs that fire on a schedule and deliver HTTP requests to configured destinations. The schedule service is available via ctx.schedule in agents and c.var.schedule in routes.

For concepts, cron routes vs platform schedules, and when-to-use guidance, see Schedules.

create(params)

Create a new schedule with optional destinations. The schedule and its destinations are created atomically: if any validation fails, the entire operation is rolled back.

ParamTypeRequiredDescription
paramsCreateScheduleParamsyesSchedule creation parameters (see below)

CreateScheduleParams fields:

FieldTypeRequiredDescription
namestringyesHuman-readable name
expressionstringyesCron expression (e.g., '0 9 * * 1-5' for weekdays at 9 AM)
descriptionstringnoDescription of the schedule's purpose
destinationsCreateScheduleDestinationParams[]noDestinations to create alongside the schedule

CreateScheduleDestinationParams fields:

FieldTypeRequiredDescription
type'url' | 'sandbox'yesDestination type
configRecord<string, unknown>yesType-specific configuration

For 'url' destinations, the config shape is:

{
  url: string;
  headers?: Record<string, string>;
  method?: string;
}

Returns: Promise<ScheduleCreateResult>

interface ScheduleCreateResult {
  schedule: Schedule;
  destinations: ScheduleDestination[];
}
 
interface Schedule {
  id: string;              // Prefixed with sch_
  name: string;
  description: string | null;
  expression: string;      // Cron expression
  due_date: string;        // ISO 8601, next scheduled execution
  created_at: string;
  updated_at: string;
  created_by: string;
}

Example

import { createAgent } from '@agentuity/runtime';
 
const agent = createAgent('ScheduleManager', {
  handler: async (ctx, input) => {
    const result = await ctx.schedule.create({
      name: 'Daily Report',
      description: 'Generate and send daily reports at 9 AM',
      expression: '0 9 * * *',
      destinations: [
        {
          type: 'url',
          config: {
            url: 'https://example.com/reports/generate',
            headers: { 'Authorization': 'Bearer token' },
          },
        },
      ],
    });
 
    ctx.logger.info('Schedule %s created, next run: %s',
      result.schedule.id, result.schedule.due_date);
 
    return { scheduleId: result.schedule.id };
  },
});

list(params?)

List all schedules with optional pagination.

ParamTypeRequiredDescription
paramsobjectnoPagination options
params.limitnumbernoMax results (max 500)
params.offsetnumbernoResults to skip

Returns: Promise<ScheduleListResult>

interface ScheduleListResult {
  schedules: Schedule[];
  total: number;
}

Example

const { schedules, total } = await ctx.schedule.list({ limit: 20 });
ctx.logger.info('Showing %d of %d schedules', schedules.length, total);
 
for (const s of schedules) {
  ctx.logger.info('%s: %s (next: %s)', s.name, s.expression, s.due_date);
}

get(scheduleId)

Get a schedule by its ID, including all configured destinations.

ParamTypeRequiredDescription
scheduleIdstringyesSchedule ID (prefixed with sch_)

Returns: Promise<ScheduleGetResult>

interface ScheduleGetResult {
  schedule: Schedule;
  destinations: ScheduleDestination[];
}
 
interface ScheduleDestination {
  id: string;           // Prefixed with sdst_
  schedule_id: string;
  type: 'url' | 'sandbox';
  config: Record<string, unknown>;
  created_at: string;
  updated_at: string;
  created_by: string;
}

Example

const { schedule, destinations } = await ctx.schedule.get('sch_abc123');
ctx.logger.info('Schedule: %s, destinations: %d', schedule.name, destinations.length);

update(scheduleId, params)

Update an existing schedule. Only provided fields are modified. If the expression changes, the due_date is automatically recomputed.

ParamTypeRequiredDescription
scheduleIdstringyesSchedule ID
paramsUpdateScheduleParamsyesFields to update (all optional)

UpdateScheduleParams fields (all optional):

FieldTypeDescription
namestringUpdated name
descriptionstringUpdated description
expressionstringUpdated cron expression

Returns: Promise<{ schedule: Schedule }>

Example

const { schedule } = await ctx.schedule.update('sch_abc123', {
  name: 'Nightly Sync',
  expression: '0 0 * * *',
});
 
ctx.logger.info('Updated. Next run: %s', schedule.due_date);

delete(scheduleId)

Delete a schedule and all of its destinations and delivery history.

ParamTypeRequiredDescription
scheduleIdstringyesSchedule ID

Returns: Promise<void>

Destinations

createDestination(scheduleId, params)

Add a new destination to an existing schedule.

ParamTypeRequiredDescription
scheduleIdstringyesSchedule ID
paramsCreateScheduleDestinationParamsyesDestination type and config

Returns: Promise<{ destination: ScheduleDestination }>

Example

const { destination } = await ctx.schedule.createDestination('sch_abc123', {
  type: 'url',
  config: {
    url: 'https://example.com/webhook',
    headers: { 'X-Source': 'agentuity' },
  },
});
 
ctx.logger.info('Destination added: %s', destination.id);

deleteDestination(destinationId)

Remove a destination from a schedule.

ParamTypeRequiredDescription
destinationIdstringyesDestination ID (prefixed with sdst_)

Returns: Promise<void>

getDestination(scheduleId, destinationId)

Get a specific destination by schedule and destination ID.

Returns: Promise<ScheduleDestination>

Delivery History

listDeliveries(scheduleId, params?)

List delivery attempts for a schedule. Each time a schedule fires, one delivery record is created per destination.

ParamTypeRequiredDescription
scheduleIdstringyesSchedule ID
paramsobjectno{ limit?: number; offset?: number }

Returns: Promise<ScheduleDeliveryListResult>

interface ScheduleDelivery {
  id: string;
  date: string;                   // ISO 8601 timestamp
  schedule_id: string;
  schedule_destination_id: string;
  status: 'pending' | 'success' | 'failed';
  retries: number;
  error: string | null;
  response: Record<string, unknown> | null;
}

Example

const { deliveries } = await ctx.schedule.listDeliveries('sch_abc123');
for (const d of deliveries) {
  ctx.logger.info('%s: %s (retries: %d)', d.date, d.status, d.retries);
  if (d.error) {
    ctx.logger.error('  Error: %s', d.error);
  }
}

getDelivery(scheduleId, deliveryId, params?)

Get a specific delivery record by its ID. Accepts optional pagination parameters to control the search window.

Returns: Promise<ScheduleDelivery>