NEW

Chainlink Data Streams have officially launched on mainnet. Sign up for early access.

Back

JavaScript code API Reference

JavaScript source code for a Functions request should comply with certain restrictions:

  • Allowed Modules: Only vanilla Deno is supported, without module imports.
  • Return Type: Must return a JavaScript Buffer object representing the response bytes sent back to the invoking contract.
  • Time Limit: Scripts must execute within a 10-second timeframe; otherwise, they will be terminated, and an error will be returned to the requesting contract.

HTTP Requests

For making HTTP requests, use the Functions.makeHttpRequest function.

Syntax

const response = await Functions.makeHttpRequest({
  url: "http://example.com",
  method: "GET", // Optional
  // Other optional parameters
})

Parameters

ParameterOptionalityDescriptionDefault Value
urlRequiredThe target URL.N/A
methodOptionalHTTP method to be used.'GET'
headersOptionalHTTP headers for the request.N/A
paramsOptionalURL query parameters.N/A
dataOptionalBody content for the request.N/A
timeoutOptionalMaximum request duration in milliseconds.3000 ms
responseTypeOptionalExpected response type.'json'

Return Object

Response TypeFieldsDescription
SuccessdataResponse data sent by the server.
statusNumeric HTTP status code.
statusTextTextual representation of HTTP status.
headersHTTP headers sent by the server in the response.
ErrorerrorIndicates an error occurred (true).
messageOptional error message.
codeOptional error code.
responseOptional server response.

Data Encoding Functions

The Functions library includes several encoding functions, which are useful for preparing data for blockchain contracts.

FunctionInput TypeOutput TypeDescription
Functions.encodeUint256Positive Integer32-byte BufferConverts a positive integer to a 32-byte Buffer for a uint256 in Solidity.
Functions.encodeInt256Integer32-byte BufferConverts an integer to a 32-byte Buffer for an int256 in Solidity.
Functions.encodeStringStringBufferConverts a string to a Buffer for a string type in Solidity.

Note: Using these encoding functions is optional. The source code must return a Uint8Array which represents the bytes that are returned on-chain.

const myArr = new Uint8Array(ARRAY_LENGTH)

Stay updated on the latest Chainlink news