Script Examples
Scripts are serverless functions that execute custom logic in response to events, schedules, or API calls. These examples demonstrate common patterns for building production-ready scripts, from simple data processing to complex event-driven workflows.
Each example includes complete, runnable code with explanations of key concepts and deployment instructions.
Examples
Tracking cryptocurrency price changes
This example demonstrates how to build a price tracking script that monitors cryptocurrency prices and generates alerts when significant price movements occur. The script fetches current prices, calculates 24-hour percentage changes, and filters results based on a configurable threshold.
JavaScript
Deployment
Deploy the script using the POST /api/sdk/v1/scripts endpoint:
Result
The script returns an object containing all price data and any alerts for symbols that exceeded the threshold. This pattern is useful for monitoring markets, triggering notifications, or feeding data to dashboards.
Transforming JSON data with dynamic operations
This example shows how to build a data transformation script that applies a series of operations to JSON data. The script supports filtering, mapping, and reducing operations, making it useful for ETL pipelines and data processing workflows.
JavaScript
Result
The script processes the input data through each transformation step and returns the final result. This pattern enables flexible data processing without modifying the script code—simply pass different transformation configurations in the event payload.
Processing webhook events
This example demonstrates a webhook handler script that processes different types of incoming events. The script uses a switch statement to route events to appropriate handler functions, making it easy to add new event types as your integration grows.
JavaScript
Result
The script processes the webhook event and returns a confirmation. This pattern is ideal for integrating with third-party services that send webhook notifications, such as payment processors, CRM systems, or messaging platforms.
Running scheduled tasks
This example shows how to build a scheduled task script that runs periodically using cron scheduling. The script fetches data, processes it, and sends notifications—a common pattern for monitoring, reporting, and data synchronization tasks.
JavaScript
Result
The script executes on schedule (e.g., every 5 minutes) and returns a summary of the processed data. Configure the schedule when creating the script using cron syntax. This pattern is useful for periodic data collection, health checks, and automated reporting.
Handling errors gracefully
This example demonstrates error handling best practices for production scripts. Always wrap risky operations in try-catch blocks and return structured error responses that help with debugging and monitoring.
JavaScript
Result
The script returns a structured response indicating success or failure. This pattern ensures your scripts fail gracefully and provide useful error information for debugging. Always log errors for monitoring and alerting purposes.
Testing Scripts
Executing a script
Test your script by sending a request to the execute endpoint:
The response includes the script's return value and execution metadata such as duration and memory usage.
Monitoring execution history
Check execution history and performance metrics:
This endpoint returns a list of recent executions with timestamps, status codes, and error messages for failed executions.
Best Practices
Resource optimization
Configure your scripts for optimal performance and cost:
- Timeout values: Set timeouts based on expected execution time (default: 30 seconds)
- Memory allocation: Start with 256 MB and increase if needed
- Caching: Cache frequently accessed data to reduce API calls
- Batching: Batch multiple API requests using
Promise.all()to reduce latency
Input validation
Validate input parameters at the start of your handler function:
Testing checklist
Before deploying to production:
- Validate all input parameters and edge cases
- Test error scenarios and timeout conditions
- Verify resource limits (memory, timeout)
- Check output format matches expected schema
- Test with production-like data volumes
See also
- Scripts - Complete script documentation
- Script Events - Event-driven script patterns
- Creating Scripts - Detailed creation guide
- Resource Examples - API integration examples
- Template Examples - Complete application templates