Here for the API?Start building with us today

Learn about DeCommas API
APIs in Web3 and How They Work
API
March 20, 2024

APIs in Web3 and How They Work

Building decentralized applications (dApps) on Web3 unlocks a world of possibilities, but it also presents developers with unique challenges such as direct and complex blockchain interactions and advanced technical knowledge. Under Web3 promise of decentralization lies a complex network — a labyrinth of protocols and codes that can be overwhelming. This is where Web3 APIs come in, acting as the essential bridge between applications and the intricate workings of blockchains.

These APIs, acting as the bridge, close the gap between applications and blockchains. They seamlessly connect applications to crucial components, such as oracles (data providers) and smart contracts (self-executing agreements) to blockchains. This simplifies tasks for developers, allowing them to focus on what matters most — building innovative dApps without getting bogged down with the technical complexities.

In connecting dApps to decentralized networks, Web3 APIs act as the intermediary, translating your app’s requests into a format the blockchain understands, and vice versa, without you having to worry about the how.

There are a lot of Web3 API providers, all of them offering different, or sometimes similar features and functionalities. DeCommas API is one of these Web3 API providers simplifying development with features like account abstraction, offering ease of use for both developers and users, providing a vast range of endpoints and supports multiple blockchain networks by default, while ensuring blazing-fast performance.

This article will dive into these Web3 APIs, how they differ from the traditional Web2 APIs and why they’re developers’ best friend. We’ll then unveil the core components of a Web3 API, explain how it functions, and even guide you through making a simple API call. Finally, we’ll summarize DeCommas API offering and outline in which cases our API stands out as best-in-class.

Key Points:

  • Web3 APIs simplify blockchain interaction for developers.
  • They act as a bridge between applications and the functionalities offered by blockchains.
  • Different Web3 API providers offer various features and functionalities.

How does API in Web3 Differ from API in Web2

The initial definition around APIs refers, in general, to Web2 APIs. But, even though there are similarities to all APIs, for example; the concept of APIs, API frameworks in use (REST, GraphQL etc.); there is a marked difference between these particular two. Here is a table that shows the difference between the two:

While Web2 APIs exchange data with a centralized network, e.g. a database or a third party service, Web3 APIs interface with decentralized networks. This difference gives Web3 APIs a high level of security compared to Web2, since centralized data may be susceptible to tampering, whereas blockchain data is never stored on a single system, making it nearly impossible for written data to be tampered with.

Why Should You Use an API When You Can Run Your Own Node?

Imagine needing fresh ingredients to cook a delicious meal. Running your own farm (like a node) gives you finely-tuned control, but it requires significant effort — acquiring land, planting seeds, and tending to the crops. In contrast, using a grocery store (like a Web3 API) provides a convenient and readily available source of ingredients, allowing you to focus on your culinary expertise (developing your application) without the weight of managing the entire agricultural process (node operation).

Based on this, while it is not an unknown fact that by utilizing Web3 APIs, aggregate development time is much reduced, allowing ideas to come to life quicker… there are other hidden gems of using Web3 APIs:

  • Cost-effectiveness: Running a node incurs ongoing expenses for hardware, software, bandwidth, and, hiring specialists. Web3 APIs offer a cost-efficient solution; you only pay for the data you use, eliminating the upfront investment and ongoing maintenance costs associated with running your own node.
  • Scalability: Web3 APIs from most providers, such as trusted providers like DeCommas API, by default, are built to handle fluctuating traffic efficiently. This means that they provide the necessary scalability systems to ensure smooth operation for your application, regardless of user base size.
  • Security: Maintaining the security of your node requires constant vigilance and expertise. A huge number of Web3 APIs are offered by reputable providers like DeCommas, a notable RPC provider for DRPC that prioritizes robust security measures. They invest heavily in infrastructure and security protocols to safeguard user data and prevent malicious attacks without you having to worry about it by yourself.

DeCommas dashboard on dRPC

DeCommas dashboard on dRPC

In conclusion, while running your node offers a certain level of control, the overwhelming benefits of Web3 APIs make them the clear choice for most use cases. Their simplicity, cost-effectiveness, scalability, and robust security empower you to focus on building innovative dApps and contribute to the burgeoning world of decentralized technologies.

Functions of Web3 API

This section focuses on terminologies used in Web3 API, as we explore how Web3 API works, all from the developer’s perspective.

When diving into the mechanics of Web3 API, understanding the fundamental terminologies is essential for understanding the core components.

While there are many providers offering Web3 API solutions, for clarity and insight, we will use DeCommas APIs as illustrative examples in this section.

1. Endpoints

Function: Endpoints serve as access points to interact with the API. These are URLs that specify the location where resources can be accessed or manipulated.

Example with DeCommas API:

  • /user_operation/{chain_name}/{user_operation_hash} — Returns extended information about the user operation by network name and operation hash.
  • /tokens/{address} — Returns the balance of ERC-20 tokens for an address across all networks.

This is not an extensive list of DeCommas API endpoints (check DeCommas API documentation for a full list), as DeCommas API currently provides 21 unique endpoints.

2. Methods

Function: Methods define the actions that can be performed on endpoints. Common methods include GET for retrieving data, POST for creating new resources, PUT for updating existing resources, and DELETE for removing resources.

Example with DeCommas API:

  • GET: This method retrieves data from an endpoint; like fetching an account balance, a transaction detail, top holders of an NFT collection, user operation or operations, token metadata, coin metadata, etc.
  • POST: This method sends data to an endpoint, often used for interacting with smart contracts.

3. Parameters

Function: These act like additional instructions passed along with the request, providing specific details about the desired action.

Example with DeCommas API:

Parameters like limit and offset can be used to specify the number of transactions to retrieve and the starting point for pagination, amongst other parameters such as api-key, and networks.

/user_operations/{address}?api-key={YOUR-API-KEY}&networks=all&limit=20&offset=10

* That is a sample query showing the use of parameters.

* Allows you to run a query for a list of user’s past operations, specifying your api-key, the networks of interest (all 15 networks covered by DeCommas API, or networks=linea,arbitrum for only linea and arbitrum, etc), the maximum number of transactions to retrieve (in descending order of date), and the starting point for pagination.

4. Request/Response Format

Function: The request format specifies how data is sent to the API, while the response format defines how data is returned from the API. Common formats include JSON (JavaScript Object Notation) and XML (eXtensible Markup Language).

Example with DeCommas API:

DeCommas utilizes JSON for both request and response formats, ensuring a standardized and machine-readable way to exchange information.

{
"status": 200,
"result": []
}

* General DeCommas API response format.

* ‘“status”: 200’ indicates a successful API call (standard HTTP response code for success), and ‘“result”: []’ gives you the actual response data (all to be contained in the square bracket array), where you can then use the information of that data for your needs.

5. Authentication/Authorization

Function: Authentication verifies the identity of the user or application making the request, while authorization determines whether the authenticated user has permission to access the requested resource.

Example with DeCommas API:

DeCommas API employs API keys for authentication, ensuring that only authorized users can access the API endpoints. This helps maintain the security and integrity of the data.

/transfers_erc20/{address}?api-key={YOUR-API-KEY}

* Sample endpoint showing the use of API key

* While the endpoint retrieves ERC-20 token transfer history associated with a specific address (you can always use any of our endpoints with API key; not only transfers_erc20), you can see the use of api-key parameter, which is highly essential for authentication and authorization.

How does a Web3 API work?

For developers, understanding how a Web3 API operates is crucial for harnessing its power in building decentralized applications. Simplified into everyday language, a Web3 API works much like a translator, converting developers’ requests into a language blockchains can comprehend and vice versa.

Let us take for example, you building a dApp. You want it to fetch data from a blockchain, execute specific functions within a smart contract, initiate a transaction, or manage user wallets:

  1. Reading Blockchain Data:
  • Developer Request: “Fetch the latest transaction data from the Ethereum blockchain.”
  • Web3 API Action: Sends a request to the blockchain, retrieves the data, and delivers it to the developer in a format they can easily use.
  1. Executing Smart Contract Functions:
  • Developer Request: “Execute a function in this smart contract to transfer tokens.”
  • Web3 API Action: Translates the request, interacts with the smart contract on the blockchain, and executes the specified function on behalf of the developer.
  1. Initiating Transactions:
  • Developer Request: “Initiate a transaction to transfer cryptocurrency from this wallet to another.”
  • Web3 API Action: Orchestrates the transaction, broadcasts it to the blockchain network, and relays the confirmation back to the developer.
  1. Managing User Wallets:
  • Developer Request: “Retrieve the current balance and transaction history for this user’s wallet.”
  • Web3 API Action: Accesses the blockchain, fetches the wallet details, and presents them to the developer for seamless integration into the dApp.
  1. Infrastructure of Web3 APIs

While Web3 APIs act as the middle ground for developers, they rely on a robust infrastructure behind the scenes to communicate with blockchains. These API providers utilize their own blockchain infrastructure to directly interact with and collect data from the blockchain. This infrastructure can consist of:

  • Self-maintained Hardware: Some API providers invest in their own physical hardware like servers to ensure maximum control and security.
  • Cloud-Based Solutions: Others leverage the power and scalability of cloud providers like Google Cloud Platform or Amazon Web Services.

Combining these options, like DeCommas API using a mix of their own hardware and cloud services, creates a secure and efficient environment for developers.

In essence, Web3 APIs act as the essential middle ground. They handle the complexities of interacting with the blockchain, such as; reading blockchain data, executing smart contract functions, sending transactions, managing user wallets, displaying portfolios, returning user operations, amongst many other things, all while empowering you as the developer to achieve these tasks easily.

API Call Example — A Simple Step-by-Step Guide to Making API Calls with DeCommas API

Getting Your Personal Mission Control API Key

Begin by signing up for an account on the DeCommas platform. Navigate to the registration page and follow the prompts to create your account.

Step 1: Visit https://build.decommas.io/

Step 2: Click “Get Started”

Step 3: Provide your email address for verification

Step 4: Check your email for the verification link, and click it to access with URL https://dashboard.decommas.io/ , your gateway to API keys.

Ensure your API keys are stored using high-level security and readily accessible for future use. Furthermore, you can consult our documentation here for further research.

Raw HTTPS Requests:

GET https://datalayer.decommas.net/datalayer/api/v1/transaction_user_operations/{chain_name}/{tx_hash}?api-key={YOUR_API_KEY}

The above is an example URL of one of the UserOps APIs. All you need to do is to use any of the available URLs in your code via the GET method in any language of your choice, and do not forget to replace placeholder values with your actual values.

Example Setup in JavaScript:

fetch(`https://datalayer.decommas.net/datalayer/api/v1/user_operations/${address}?api-key=${YOUR_API_KEY}`)
.then(response => response.json())
.then(data => {
const status = data.status;
const results = data.result;
console.log(status);
console.log(results);
})
.catch(error => {
console.error(`Error. Reason for Error: ${error}`);
});

NOTE:

  1. The response will always be in JSON. You can always use any language of your choice to write your request code.
  2. The example given here is just a sample use of DeCommas APIs. DeCommas provides 21 market-unique endpoints covering 15 networks.

Networks covered by DeCommas API are:

  • Mainnet
  • Arbitrum
  • Arbitrum Nova
  • Avalanche
  • Optimism
  • BSC
  • Fantom
  • Gnosis
  • Polygon
  • Polygon zkEVM
  • Base
  • opBNB
  • Linea
  • Mantle
  • Scroll

You can see the list and use cases here.

Summary

Web3 APIs serve as the crucial link between applications and the complex world of blockchains. These APIs act as translators, enabling applications to interact with features like smart contracts and on-chain data, empowering you to harness the power of Web3 without diving deep into technical complexities.

While the allure of running your node for data extraction is understandable, Web3 APIs offer compelling alternatives, such as cost-effectiveness, scalability, and robust security — factors often challenging to manage with an individual node setup.

DeCommas API stands out as a developer-centric Web3 API provider. With over 21 endpoints (including unique Account Abstraction features), seamless interaction with over 15 blockchains, and response times below 150ms, we have combined extensive functionality, robust multi-chain support, efficiency, and unparalleled speed for your gains. To get started, visit here now!