0%
Overview page background
HomeOverviewsWeb3 Tools
What is the Alchemy SDK?

What is the Alchemy SDK?

Alchemy headshot

Written by Alchemy

Brady Werkheiser headshot

Reviewed by Brady Werkheiser

Published on 2023-07-107 min read

One of the primary pain points for web3 developers is understanding how to interface with the blockchain:

  1. How do you read information from the Ethereum network?

  2. How do you create new smart contracts and publish them to the blockchain? 

  3. What do you need to do to properly manage gas and pending transactions?

The Alchemy SDK is the most comprehensive tool available today to make the process of interfacing with the blockchain painless.

With only a single line of code, you can get information about the latest block, identify all transactions from a certain user, or find all the owners of an NFT collection. 

On top of that, it’ll allow you to build using WebSockets, create and deploy smart contracts, and get access to Alchemy’s best-in-class infrastructure used by over 70% of the largest dApps and DeFi projects in the world. 

The best thing is - it’s completely free to use, with over 12 million requests per month at no charge simply by setting up an Alchemy account!

Are you:

  • Interested in building a web3 dApp in under a week? 

  • Hoping to speed up your web3 development workflow and skip 100s of overhead?

  • Looking to become a web3 developer?

One of the best ways to build in web3 is getting started with the Alchemy SDK, a must-have web3 developer tool. 

There’s an endless number of new concepts, blockchains, and tools to discover.

What’s more, the most popular tooling and chains can change on a monthly basis.

It can all be a bit overwhelming.

The Alchemy SDK is the best way to start developing in web3!

What are Web3.js and Ethers.js?

Web3.js and Ethers.js are two open-source Javascript libraries developers use to communicate with blockchain nodes. Simply by adding a public or private RPC endpoint URL for a node provider, developers can start sending requests to and receive responses from blockchains like Ethereum.

Instead of having to specify a complicated request as a JSON object sent in the body of a request, Web3.js and Ethers.js make it easier to interact with the exposed endpoints by simply calling a single Javascript method and passing in the appropriate parameters.

For context, a blockchain node is a piece of software that permits communication to the rest of the blockchain. Nodes run on servers owned by individuals around the globe, and communicate to each other much in the same way internet servers communicate to permit reading and writing to the rest of the blockchain.

A blockchain node provider is a platform like Alchemy that offers access to remote nodes, typically for a small fee at high volumes. This allows most developers to send and receive requests from the blockchain without needing to run their own nodes, a massive amount of logistical overhead for most developers.

What are the drawbacks of using Ethers.js and Web3.js?

Though these APIs are an excellent starting point and cover roughly 70% of use cases, they were designed when Ethereum launched in 2015 and are not optimized for modern web3 developer needs.

For instance, neither Web3.js or Ethers.js support the following use cases:

  • Retrieve all the NFTs minted in a NFT collection

  • Return all the ERC-1155 asset transfers from a particular address

  • Push notifications whenever activity happens involving a particular address

For these last 30% of use cases, you’ll need custom features or endpoints built by a blockchain developer platform like Alchemy.

The Alchemy SDK gives you the web3 API endpoints you need to build modern blockchain applications, and all it takes to get started is a one-line-of-code.

7 Reasons Why Alchemy's Web3 SDK Makes Development Easier

Let’s go over why the Alchemy SDK is going to save you hundreds of hours of web3 development time, and make it so that you can retrieve exactly the information you need from the blockchain with no effort.

1. Feature Parity with Ethers.js

The Alchemy SDK is built directly on top of the Ethers.js Provider, the module responsible for requesting information from the blockchain. 

If you’re already using the Ethers.js Provider, that means integrating the Alchemy SDK is zero effort - simply replace your `ethers` object with `alchemy.core` and your project will continue working! 

This also means that all the Ethereum methods you’ve been using from Ethers will follow the exact same syntax, making onboarding painless.

2. Code Simplification

Using the Alchemy SDK to develop web3 projects simplifies your project’s code, making it easier to write, debug, and manage. Here is a before and after look at making an eth_blockNumber RPC request.

eth_blockNumber with Ethers.js

This is how eth_blockNumber RPC requests are made with the default ethers.js library:

Copied
curl --request POST \ --url https://eth-mainnet.alchemyapi.io/v2/demo \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ --data ' { "id": 1, "jsonrpc": "2.0", "method": "eth_blockNumber" }

eth_blockNumber with Alchemy’s Blockchain SDK

Alchemy’s blockchain SDK simplifies code, enabling devs to ship faster. With the Alchemy SDK, the same eth_blockNumber RPC request takes one line instead of 10.

alchemyProvider.getBlockNumber()

3. Access to Alchemy’s Enhanced APIs

Unlike Ethers.js, Alchemy has a whole suite of custom APIs that makes developer’s lives easier. Let’s look at an example - the Transfers API.

Say that you want to search for all transactions made by a single Ethereum address.

Because the entire blockchain is stored as a series of sequential blocks containing the transactions submitted when that block was included on the blockchain, this is a difficult task using the Ethereum API. 

Unlike a traditional database, you can’t simply sort by the “from” address field.

Instead, you’d have to search every single block from Ethereum’s Genesis Block to the present day (over 15 million blocks), and check every transaction to isolate the transfers sent from a single Ethereum address. 

This requires spinning up a custom node, writing code to scan the blockchain, and making millions of requests via persistent infrastructure to continually index transactions per address.

Not so with Alchemy!

Using our Transfers API, we’ve done all of this hard work for you. 

Just call a single endpoint with the address you’d like to track and the blocks you’d like transactions from, and we’ll give you your results in under a second.

Copied
const transfers = await alchemy.core.getAssetTransfers({ category: ["external"], fromBlock: "0x0", fromAddress: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", });

4. Access to Alchemy’s NFT API

Similarly, NFTs were never built into the original Ethereum API. As a result, there are no methods currently available in Ethers.js or Web3.js libraries for tracking NFTs minted per contract, finding the current owners of an NFT, retrieving metadata for all NFTs in a collection, and many other NFT API use cases. 

Alchemy’s NFT API and SDK work together to support a comprehensive list of NFT API endpoints with a simple SDK, simplifying projects like building an NFT marketplace from 1000s of lines of code by a factor of ten, and reduces a complicated server stack to a single machine.

Here are some Alchemy NFT API examples using Alchemy's web3 SDK.

Get All Owners of an NFT Contract

alchemy.nft

  .getOwnersForContract("0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D");

Get the NFTs Owned by vitalik.eth

alchemy.nft.getNftsForOwner("vitalik.eth");

Print all Spam NFT Contracts

alchemy.nft.getSpamContracts()

5. Robust WebSockets Handling

Though WebSockets are supported by Ethers.js and Web3.js, they’re unreliable. WebSockets will close for numerous reasons, including:

  1. Server-side buffers being overfilled

  2. Close requests being sent by either the client or server

  3. Bad network connections

Every time WebSockets go down, it’s on the user to re-open them, resend missed requests, and manage the lifecycle.

Instead, using WebSockets with the Alchemy SDK provides robust support by:

  1. Re-opening WebSockets when they’re closed automatically

  2. Backing up requests that were meant to be sent during downtime

  3. Re-sending requests as soon as a new WebSocket connection is opened

6. Reliability and Scalability

When you use the Alchemy SDK, you’ll automatically get all the benefits of Alchemy’s best-in-class infrastructure, with all major NFT marketplaces, 70% of top dApps, and 70% of TVL relying on Alchemy’s  platform. 

Developers at large organizations choose Alchemy as their primary node provider because when you’re running a platform with millions of users, even a few minutes of downtime means millions of dollars in lost revenue.

Alchemy’s superior reliability and scalability means that you’re trusting your infrastructure and customer satisfaction to the same backend that powers OpenSea, Meta, Chainlink, dYdX, and more.

7. Data Correctness

Alchemy has the best data accuracy among web3 node providers, returning inconsistent block results orders of magnitude lower than its competitors.

Data accuracy is critical for proper transaction handling - otherwise, you might send your transactions to blocks that are no longer valid or return incorrect data to your users!

Number of inconsistent blocks

How do I use the Alchemy SDK?

With just a couple of lines of code, you’ll have a working Alchemy object that allows you to make any request to the blockchain.

import { Alchemy } from "alchemy-sdk";

const alchemy = new Alchemy();

Just a few more lines of code will give you access to over 12 million free requests a month and 10 networks across the biggest EVM blockchains such as Optimism, Polygon, Arbitrum, and Astar.

Step 0: Set up an Alchemy Account

If you don’t already have an Alchemy account, you can sign up for free.

Step 1: Create your App and API Key

Next, generate an API key by creating a new app. This will allow you to make requests to the Goerli test network.

Here’s how to create an app and API key:

  1. Sign in to Alchemy

  2. Hover over “Apps” in the nav bar

  3. Click “Create App”

  4. Name your app (we chose "My First NFT!")

  5. Write a short description

  6. Select “Staging” for the Environment (used for your app bookkeeping)

  7. Choose “Goerli” for your network.

  8. Click “Create app”

That’s it!

Step 2: Grab Your API Key

To send and receive information from the blockchain, your application needs an API key. 

  1. Click on “API Key” next to your app 

  2. Copy your API key 

  3. Plug it into your Alchemy SDK

This API key gives you access to 300 million free compute units a month! That’s roughly 12 million transactions!

Step 3: Integrate your API key Into Your Alchemy SDK

Here’s the code to start using your API key on an Alchemy SDK object set up to send requests to the Goerli test network!

Copied
import { Alchemy, Network } from "alchemy-sdk";‍ const settings = { apiKey: "<API_KEY>", // Replace with your Alchemy API Key. network: Network.ETH_GOERLI, }; const alchemy = new Alchemy(settings);

Web3 SDK Examples

Here are some quick examples for using Alchemy’s web3 SDK with different API endpoints supported by Alchemy including Ethereum APIs, enhanced APIs, NFT APIs, and WebSockets.

Use Alchemy’s Web3 SDK with Core and Enhanced APIs

Want to make requests to the standard Ethereum API? It’s insanely easy once you’ve created an Alchemy object. Here’s some examples: 

1. Access standard Ethers.js JSON-RPC node request

alchemy.core.getBlockNumber().then(console.log);

2. Access the balance for Vitalik.eth

alchemy.core.getBalance("vitalik.eth", "latest").then(console.log);

3. Access Alchemy Enhanced API requests

alchemy.core.getTokenBalances("0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE").then(console.log);

Use Alchemy’s SDK and NFT API

Interested in leveraging the features of Alchemy’s NFT API? Here’s some quick code examples: 

1. Count How Many NFTs an Address Owns

alchemy.nft.getNftsForOwner('vitalik.eth').then(nfts => {

  console.log(nfts.totalCount);

});

2. Print NFT Metadata Returned in the Response

alchemy.nft.getNftMetadata(

  "0x5180db8F5c931aaE63c74266b211F580155ecac8",

  "1590"

).then(console.log);

3. Print Total NFT Count Returned in the Response

alchemy.nft

  .getOwnersForContract("0x61fce80d72363b731425c3a2a46a1a5fed9814b2")

  .then(console.log);

Use the Alchemy SDK with WebSockets

There are a number of unique features on WebSockets that will allow you to receive updates pushed directly to your client whenever a certain update happens on the blockchain, such as a new block being mined, logs matching a certain filter are emitted, or a pending transaction is confirmed on the blockchain.

1. Listen to All New Pending Transactions

alchemy.ws.on('block', res => console.log(res));

2. Listen to Only the Next Transaction on the USDC Contract

Copied
alchemy.ws.once( { method: 'alchemy_pendingTransactions', toAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' }, res => console.log(res) );

3. Remove All Listeners

alchemy.ws.removeAllListeners();

Start Using the Alchemy SDK

The Alchemy SDK dramatically simplifies web3 development lifecycle, expands functionality, and is supported by Alchemy’s best-in-class reliability, scalability, and data accuracy.

Upgrade your web3 tech stack with the new Alchemy SDK.

Overview cards background graphic
Section background image

Build blockchain magic

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get your API key