Skip to main content

Getting Started with Functions

Updated on
Oct 10, 2024

Overview

Functions enable you to quickly build, deploy, and use serverless functions without the need to manage infrastructure. Your Functions can be used as destinations for your Streams, and can also be activate via API.

Functions Features


  • API Ready: Your functions are automatically exposed as an API, ready to be called from your front-end or other services.
  • Performance at Scale: Our globally balanced, auto-scaling infrastructure ensures smooth operation, even at peak loads.
  • Blockchain Optimized: Use your function as a destination for Streams, and it will automatically activate when new data is piped in from your Stream. You can also activate your function via API and optionally specify a specific blockchain dataset to access within your function during the activation.
  • Storage Access: Access and manage your Storage data seamlessly within your function.
  • Cost Effective: Pay for only what you use, with no upfront costs or long-term commitments.

Want to see Functions in action? Check out our Functions Library for sample code and walkthroughs.

Feature Availability and Cost

Functions beta is available on all QuickNode rate plans. We offer dedicated support and custom integrations for teams with unique requirements. Contact our team for more information.

You can create an unlimited number of functions to meet your needs, however it is important to remember that the minimum activation time for any function is 100ms.

QuickNode planFreeStarterGrowthBusinessEnterprise
Included GB-sec5001,0003,0005,000Custom, tiered pricing*
Price per GB-secn/a$0.0000159$0.0000159$0.0000159Custom, tiered pricing*
API DatasetsBlocks, Transactions, Logs, and ReceiptsBlocks, Transactions, Logs, and ReceiptsBlocks, Transactions, Logs, Receipts, and TracesBlocks, Transactions, Logs, Receipts, Traces, and all-in-one DatasetsCustom datasets
Activate via Streams
Activate via API

Understanding GB-sec and Billing

GB-sec (Gigabyte-seconds) is an industry-standard metric we have adopted to measure and bill for Functions usage. It represents the amount of compute resources consumed by your function over time.

How GB-sec is Calculated:

  • Memory Usage: The amount of memory allocated to your function (128MB-512MB). This value is a function configuration and can be modified in the Functions UI or via API.
  • Execution Time: The duration your function runs, with a minimum of 100ms per activation.
GB-sec = (Memory Allocated in GB) * (Execution Time in Seconds)

If your function completes in less than 100ms, it will be billed for 100ms.

Example Calculations:

  1. A function configured with a 128MB memory allocation that runs for 200ms:
    GB-sec = (128/1024 GB) * (0.2 seconds) = 0.025 GB-sec

  2. A function configured with a 256MB memory allocation that runs for 50ms (rounded to 100ms minimum):
    GB-sec = (256/1024 GB) * (0.1 seconds) = 0.025 GB-sec

  3. A function configured with a 512MB memory allocation that runs for 1.5 seconds:
    GB-sec = (512/1024 GB) * (1.5 seconds) = 0.75 GB-sec

info

By understanding GB-sec, you can optimize your functions for both performance and cost-effectiveness. Consider balancing memory allocation and execution time to make the most of your included GB-sec and minimize additional charges.

Access

Access Functions through the QuickNode Developer Portal.

Functions Activation

Functions can be activated in 3 ways:

  • Test activations: Your function can be activated manually within the function editor inside the QuickNode Developer Portal.
  • Streams activation: You can choose your function as a destination for a Stream, and it will be activated automatically when data is delivered. Learn more about functions as a destination for Streams.
  • API activation: On the overview screen within the QuickNode Developer Portal for your function, you will find the unique API endpoint for your function. You can call this endpoint directly to activate your function. Learn more about the datasets available for API activations.

Supported Chains

Functions activated by API requests can automatically access blockchain data from any of the supported chains and networks mentioned below. For teams with unique requirements, contact our team for more information. Otherwise, our self-serve experience has you covered on the following chains:

ChainMainnetTestnets
ArbitrumSepolia
Arbitrum Nova
Avalanche C-ChainFuji
BaseSepolia
BeraComing soonbArtio
BlastSepolia
BNB Smart ChainTestnet
Celo
CyberSepolia
EthereumHolesky, Sepolia
Fantom
Fraxtal
Gnosis
Immutable XTestnet
MantleSepolia
Mode
OptimismSepolia
PolygonAmoy
Polygon zkEVM
ScrollTestnet
TRON
zkSyncSepolia
XaiSepolia
Zora

Function Configuration


  • Function name: The name of the function.
  • Runtime: Choose from Node.js v20 or Python v3.11.
  • Description: You can add a description to your function. It supports markdown.
  • Timeout limit: How long your function can run before timing out. The range is 5 to 60 seconds.
  • Memory allocated: The amount of memory allocated to this function. The range is 128MB-512MB.

function settings

Function Editor

You can code your function in the functions code editor, but if you want to include dependencies, you can upload your function as a Zip file.

Code View

Write code that will execute when data is delivered by Streams, or when you activate your function via API.

Within the editor, you can test your function against blockchain datasets and view activation results.

function code editor

Within the code view, your function can access all the packages that are part of the selected runtime.

Upload Zip

If you want to add additional dependencies use more packages in your function, you can switch to the .zip file view and upload your complete function as a zip file. Learn more about how to create and upload a zip file.

function zip upload

Using Key-Value Store with Functions

Key-Value Store can be accessed and managed seamlessly within a function. You can create new lists and key-value sets, add and remove items from your lists, update key-value sets, retrieve set value, and check if data matches an item on your list. To learn more about Key-Value Store, please visit the Key-Value Store documentation.

Available Key-Value Store features within functions

Lists


  • qnUpsertList: Creates or updates a new list in Key-Value Store.
  • qnGetList: Retrieves all items from a specific list in Key-Value Store.
  • qnGetAllLists: Retrieves all lists in Key-Value Store.
  • qnAddListItem: Adds an item to a specific list in Key-Value Store.
  • qnRemoveListItem: Removes an item from a specific list in Key-Value Store.
  • qnContainsListItem: Checks if an item exists in a specific list in Key-Value Store.
  • qnDeleteList: Deletes a specific list in Key-Value Store.

Sets


  • qnAddSet: Creates a key-value set in Key-Value Store.
  • qnBulkSets: Creates and removes bulk key-value sets in Key-Value Store.
  • qnDeleteSet: Deletes a key-value set from Key-Value Store.
  • qnGetSet: Retrieves the value of a specific key from Key-Value Store.
  • qnListAllSets: List all keys for sets in Key-Value Store.

Example function code for Key-Value Store

The function code below demonstrates how Key-Value Store lists and sets can be used within your function.

async function main(params) {
const results = {};

try {
// Test qnGetAllLists
results.qnGetAllLists = await qnLib.qnGetAllLists();

// Test qnDeleteList
results.qnDeleteList = await qnLib.qnDeleteList("testList");

// Test qnAddListItem
results.qnAddListItem = await qnLib.qnAddListItem("testList", "testItem");

// Test qnContainsListItem
results.qnContainsListItem_before_remove = await qnLib.qnContainsListItem("testList", "testItem");

// Test qnRemoveListItem
results.qnRemoveListItem = await qnLib.qnRemoveListItem("testList", "testItem");

// Test qnContainsListItem
results.qnContainsListItem_after_remove = await qnLib.qnContainsListItem("testList", "testItem");

// Test qnGetList
results.qnGetList = await qnLib.qnGetList("testList");

// Test qnUpsertList
results.qnUpsertList = await qnLib.qnUpsertList("testList", { add_items: ["item1"], remove_items: ["item2"] });

// Test qnDeleteSet
results.qnDeleteSet = await qnLib.qnDeleteSet("testSet");

// Test qnAddSet
results.qnAddSet = await qnLib.qnAddSet("testSet", "testValue");

// Test qnGetSet
results.qnGetSet = await qnLib.qnGetSet("testSet");

// Test qnListAllSets
results.qnListAllSets = await qnLib.qnListAllSets();

// Test qnBulkSets
results.qnBulkSets = await qnLib.qnBulkSets({ add_sets: { key1: "value1" }, delete_sets: ["key2"] });

} catch (error) {
console.error("Error during method tests:", error);
}

// Return the results
return {
message: "Method test results",
results: results
};
}

Note

The function main must be async if you are accessing Key-Value Store within your function.

Functions Dashboard

Within the QuickNode developer portal, you can view details about your functions, including metrics, history, and logs. You can also edit and test your functions.

Each function generates a unique rest API endpoint that can be used to activate the function via API.

We ❤️ Feedback!

If you have any feedback or questions about this documentation, let us know. We'd love to hear from you!

Share this doc