WebSocket API

For this guide we'll be using JavaScript and the ws library to handle the WebSocket connection.

Connect to SEDA Fast

Once you have your API key you can open a WebSocket connection to SEDA Fast. After establishing the connection, the server sends an authorized message if the API key is valid. You must wait for this message before sending execution or feed requests.

Example code:

const WebSocket = require("ws");

const ws = new WebSocket("wss://fast-ws.mainnet.seda.xyz/ws/v1", {
	headers: { Authorization: `Bearer ${process.env.SEDA_FAST_API_KEY}` },
});

ws.on("open", () => console.log("πŸ”Œ Connected"));

ws.on("message", (data) => {
	const msg = JSON.parse(data.toString());

	// Uncomment this line to see the full message
	// console.log("πŸ“¨", JSON.stringify(msg, null, 2));

	if (msg.method === "authorized") {
		console.log("βœ… Authorized");

		// Close the connection after receiving the authorized notification
		ws.close(1000, "closed by client");
	}

	if (msg.error) {
		console.error("❌", msg.error.code, msg.error.message);
	}
});

ws.on("error", (e) => console.error("❌", e));

ws.on("close", (code, reason) => {
	console.log("πŸ”Œ Disconnected", code ? `(${code}${reason ? `: ${reason}` : ""})` : "");
});

Executing a Single Request

Once the authorisation works it is time to execute an Oracle Program.

Understanding the Response

A successful response includes:

Key fields:

  • id - The JSON-rpc id of the request.

  • result.data.dataRequest - Details of the Oracle Program execution request (program IDs, inputs, gas limits)

  • result.data.dataResult.result - The hex-encoded final result from the Oracle Program

  • result.data.dataResult.exitCode - 0 indicates success, non-zero indicates an error

  • result.data.dataResult.gasUsed - Total gas consumed during execution

  • result.data.dataResult.drId - Unique identifier for this data request

  • result.data.signature - Cryptographic signature for verification

  • result.data.result - Convenient top-level access to the result (same as dataResult.result)

circle-info

Additional fields may be included in the response depending on the parameters used. The WebSocket API supports the same parameters as the REST API. See the REST API Reference for complete details on all available parameters and response fields.

Failed Execution Error Handling

In the event that the message is valid and the request succeeds, but the execution of the Oracle Program fails, the WebSocket service sends an error response for the JSON-RPC request id with a "FailedExecutionResponse" tag.

Example failed execution response

When debugging failures, the response can include additional fields like "stderr" and "stdout" to help identify the issue (use the includeDebugInfo parameter to enable these).

WebSocket Error Handling

Any other errors, such as invalid parameters or invalid JSON have a smaller error response with a code and the error message.

Example

For a complete list of error codes and their meanings, see the Error Codes section in the WebSocket API Reference.

Managing a Feed

To receive continuous updates you can start a feed. A feed continues running and sending feed.result messages until:

  • The client sends feed.stop for the feed

  • The WebSocket connection is closed

It is similar to executing a single request:

Understanding a Feed Result

A successful response is very similar to the feed.execute response, just wrapped in another object that contains the feed id for this result:

Feed Error Handling

In the event that the execution of the Oracle Program in the feed fails, the WebSocket service sends an error response for the JSON-rpc request id. This is similar to feed.execute, just wrapped in another object that contains the feed id for this failed execution.

Last updated

Was this helpful?