globe-pointerWebsocket

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.

Last updated

Was this helpful?