Functions
Write and deploy serverless JavaScript functions on Snag Stratus. Schedule via cron, trigger via webhooks, or chain to onchain subscriptions for custom reward logic.
Using an AI coding assistant? Connect Cursor, Claude, or Copilot to Snag Docs for context-aware help. Learn how.
Overview
Stratus functions enable developers to write and execute JavaScript functions in a serverless environment. These functions:
- Scale horizontally
- Run in isolated VMs
- Have web access for external integrations
- Support complex logic combinations
Functions can interact with external services, allowing you to integrate with your own database, subgraphs, and other services.
Invocation Types
Schedule Invocation
While there's no maximum schedule limit, the minimum resolution is 1 minute. Schedules with smaller intervals will fail.
Webhook Invocation
To invoke functions via webhook:
- Create a Stratus-scoped API key in the admin panel
- Use the unique webhook URL assigned to your function
curl --request POST \
--url https://admin.snagsolutions.io/api/stratus/functions/<functionID>/invoke \
--header 'Content-Type: application/json' \
--header 'X-API-KEY: <stratus_api_key>' \
--data '{
"organizationId": "org-1234",
"websiteId": "site-5678",
"input": {
"foo": 123,
"bar": "example value"
}
}'Subscription Invocation
Functions linked to subscriptions execute automatically when subscription events fire. Unlike webhooks, subscription-based invocations receive a single payload per event.
Writing Functions
Basic Structure
All Stratus functions must follow these rules:
Only one handler function can be exported per file
Handlers must be asynchronous
Functions must accept input and output parameters
module.exports.handler = async (input, output) => {
// Your function logic here
};The Output Class
The output parameter provides methods for managing function results and actions:
setResultfunctionSets the function result data (called once before buildOutput)
setResult(resultObject: Record<string, any>): voidsetErrorfunctionLogs a failure and stops future executions
setError(error: string): voidaddTransactionfunctionAdds a transaction to the execution queue (requires relayer connection)
addTransaction(tx: Pick<SendTransactionParameters, 'to' | 'data' | 'value'>): voidbuildOutputfunctionFinalizes and returns the function output (required)
buildOutput(): Record<string, any>Executing Transactions
To execute transactions, your function must be connected to a relayer in your account.
Transaction Rules
- Maximum 500 transactions per function run
- Transactions execute in the order they're added
- All transactions use the connected relayer
Approved Modules
Only pre-approved npm modules can be used in Stratus functions. Contact Snag Solutions if you need additional modules.
Currently approved modules:
Example Function
const axios = require('axios');
module.exports.handler = async (input, output) => {
try {
// Make an external API call
const response = await axios.get('https://api.example.com/data');
// Set the result
output.setResult({
data: response.data,
timestamp: Date.now()
});
// Add a transaction if needed
output.addTransaction({
to: '0x123...',
data: '0x456...',
value: '0'
});
// Return the built output
return output.buildOutput();
} catch (error) {
output.setError(`Function failed: ${error.message}`);
return output.buildOutput();
}
};