The provided TypeScript code snippet is a template for deploying a script to the Acurast network through Apillon. This script is designed to establish a WebSocket connection to Acurast's decentralized network, which is essential for enabling communication between your script and the Acurast processors. Here's a breakdown of how this template works and how it fits into the deployment process:
declare const _STD_: any;
_STD_.ws.open(
[
"wss://websocket-proxy-1.prod.gke.acurast.com/",
"wss://websocket-proxy-2.prod.gke.acurast.com/",
],
() => {
// Success callback: Code to execute when the WebSocket connection is successfully opened
},
(error) => {
// Error callback: Code to handle any errors that occur while opening the WebSocket connection
}
);
The request body should contain all the data you wish to send to the cloud function before it is executed.
Example request body:
HTTP POST
{
"method": "buckets"
}
This template and deployment process allow you to leverage Acurast's decentralized infrastructure for running your Node.js scripts, ensuring they are accessible and callable through the Apillon gateway. For more detailed runtime environment documentation, you can refer to the Acurast Developer Docs.
-
_STD_
Object: This is a special object provided by the Acurast runtime environment. It exposes various functionalities, including WebSocket operations, environment variable access, and more. -
WebSocket Connection: The
ws.open
method is used to establish a connection to the Acurast network. The URLs provided are endpoints for the WebSocket proxies that facilitate communication with the network. -
Callbacks: The code includes placeholders for success and error callbacks. You can define what actions to take when the connection is successfully established or if an error occurs.
When a JSON payload is sent to this job, the received object has the following structure:
{
body: object;
headers: Record<string, string>;
path: string;
queryStringParameters: Record<string, string>;
multiValueQueryStringParameters: { [name: string]: string[]; };
pathParameters: Record<string, string>;
}
body
: This property contains the parsed JSON payload from the request. ThesafeJsonParse
function is used to safely parse theevent.body
, falling back to the rawevent.body
if parsing fails.headers
: This object includes all the HTTP headers sent with the request. Headers can be used for authentication, content type specification, and more.path
: This string represents the path of the request URL. It can be used to determine the specific endpoint that was called.queryStringParameters
: This object contains key-value pairs of query string parameters from the request URL. These parameters are typically used to pass additional data to the server.multiValueQueryStringParameters
: Similar toqueryStringParameters
, but allows for multiple values per key. This is useful when a query parameter can have multiple values.pathParameters
: This object holds any parameters that are part of the URL path. These are often used in RESTful APIs to identify resources.
-
Edit the Script: You can modify the
index.ts
file and add additional files or folders as needed for your application logic. -
Environment Variables: Access environment variables using
_STD_.env["APILLON_API_KEY"]
. This is crucial for securely managing sensitive information like API keys. You can modify the environment variables through the Apillon developer console. -
Build the Script:
- Run
npm run i
to install dependencies. - Run
npm run build
to compile the TypeScript code into a JavaScript bundle (index.bundle.js
).
- Run
-
Deploy the Script:
- Via Apillon Dashboard: Upload the
index.bundle.js
file to the Apillon dashboard at Apillon Cloud Functions. - Via API: Alternatively, you can deploy the script using the Apillon API. Documentation for this process is available at Apillon Web3 Cloud Functions and Apillon Cloud Functions API.
- Via SDK: You can also deploy and manage cloud functions using the Apillon SDK. Documentation for this process is available at Apillon SDK.
- Via CLI: You can also deploy the script using the Apillon CLI. Documentation for this process is available at Apillon CLI.
- Via Apillon Dashboard: Upload the
-
Obtain Execution Endpoint: After deploying, wait a few minutes. You will obtain the callable endpoint to execute the job on the Apillon dashboard.
Once your cloud function is deployed and running, Apillon provides you with a gateway URL that you are able to call via an HTTP POST request. The URL can be found on the Apillon developer console when opening a specific cloud function, or by using the Cloud Functions API.