Alchemy SDK Quickstart

The easiest way to connect your dApp to the blockchain and get the power of Alchemy’s infrastructure. Just download, write two lines of code, and go.

The Alchemy SDK is the most comprehensive, stable, and powerful Javascript SDK available today to interact with the blockchain.

It supports the exact same syntax and functionality of the Ethers.js AlchemyProvider and WebSocketProvider, making it a 1:1 mapping for anyone using the Ethers.js Provider. However, it adds a significant amount of improved functionality on top of Ethers, such as easy access to Alchemy’s Enhanced and NFT APIs, robust WebSockets, and quality-of life improvements such as automated retries.

The SDK leverages Alchemy’s hardened node infrastructure, guaranteeing best-in-class node reliability, scalability, and data correctness, and is undergoing active development by Alchemy’s engineers.

Getting started

Check out the full Github repo here:

GitHub - alchemyplatform/alchemy-sdk-js: The easiest way to connect your dApp to the blockchain.

github.comgithub.com

GitHub - alchemyplatform/alchemy-sdk-js: The easiest way to connect your dApp to the blockchain.

1. Install the SDK:

$npm install alchemy-sdk

2. After installing the app, you can then import and use the SDK:

SDK.js
1const { Network, Alchemy } = require('alchemy-sdk');
2
3// Optional Config object, but defaults to demo api-key and eth-mainnet.
4const settings = {
5 apiKey: '<YOUR_API_KEY_HERE>', // Replace with your Alchemy API Key.
6 network: Network.ETH_MAINNET, // Replace with your network.
7};
8
9const alchemy = new Alchemy(settings);

The public “demo” API key may be rate limited based on traffic. To create your own API key, sign up for an Alchemy account here and use the key created on your dashboard for the first app.

The Alchemy object returned by new Alchemy() provides access to the Alchemy API. An optional config object can be passed in when initializing to set your API key, change the network, or specify the max number of retries.

3. Make requests using the Alchemy SDK:

SDK.js
1import { Network, Alchemy } from 'alchemy-sdk';
2
3// Optional Config object, but defaults to demo api-key and eth-mainnet.
4const settings = {
5 apiKey: 'demo', // Replace with your Alchemy API Key.
6 network: Network.ETH_MAINNET, // Replace with your network.
7};
8
9const alchemy = new Alchemy(settings);
10
11// Access standard Ethers.js JSON-RPC node request
12alchemy.core.getBlockNumber().then(console.log);
13
14// Access Alchemy Enhanced API requests
15alchemy.core.getTokenBalances("0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE").then(console.log);
16
17// Access the Alchemy NFT API
18alchemy.nft.getNftsForOwner('vitalik.eth').then(console.log);
19
20// Access WebSockets and Alchemy-specific WS methods
21alchemy.ws.on(
22 {
23 method: 'alchemy_pendingTransactions'
24 },
25 res => console.log(res)
26);

The Alchemy SDK currently supports five different namespaces, including:

  • core: All commonly-used Ethers.js Provider methods and Alchemy Enhanced API methods
  • nft: All Alchemy NFT API methods
  • ws: All WebSockets methods
  • transact: All Alchemy Transaction API methods
  • notify: CRUD endpoints for modifying Alchemy Notify Webhooks

If you are already using Ethers.js, you should be simply able to replace the Ethers.js Provider object with alchemy.core and it should just work.

The Alchemy SDK also supports a number of Ethers.js objects that streamline the development process:

  • Utils: Equivalent to ethers.utils, this provides a number of common Ethers.js utility methods for developers.
  • Contract: An abstraction for smart contract code deployed to the blockchain.
  • ContractFactory: Allows developers to build a Contract object.
  • Wallet: An implementation of Signer that can sign transactions and messages using a private key as a standard Externally Owned Account.

To contribute to the Alchemy SDK, visit our Github or click here to download the files.

Alchemy SDK Example Requests

Getting the NFTs owned by an address

SDK.js
1import {
2 NftExcludeFilters,
3 Alchemy
4} from 'alchemy-sdk';
5
6const alchemy = new Alchemy();
7
8// Get how many NFTs an address owns.
9alchemy.nft.getNftsForOwner('vitalik.eth').then(nfts => {
10 console.log(nfts.totalCount);
11});
12
13// Get all the image urls for all the NFTs an address owns.
14async function main() {
15 for await (const nft of alchemy.nft.getNftsForOwnerIterator('vitalik.eth')) {
16 console.log(nft.media);
17 }
18}
19
20main();
21
22// Filter out spam NFTs.
23alchemy.nft.getNftsForOwner('vitalik.eth', {
24 excludeFilters: [NftExcludeFilters.SPAM]
25}).then(console.log);

Getting all the owners of the BAYC NFT

javascript
1import {
2 Alchemy
3} from 'alchemy-sdk';
4
5const alchemy = new Alchemy();
6
7// Bored Ape Yacht Club contract address.
8const baycAddress = '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D';
9
10async function main() {
11 for await (const nft of alchemy.nft.getNftsForContractIterator(baycAddress, {
12 // Omit the NFT metadata for smaller payloads.
13 omitMetadata: true,
14 })) {
15 await alchemy.nft
16 .getOwnersForNft(nft.contract.address, nft.tokenId)
17 .then((response) =>
18 console.log("owners:", response.owners, "tokenId:", nft.tokenId)
19 );
20 }
21}
22
23main();

Get all outbound transfers to a provided address

ethers.js
1import { Alchemy } from 'alchemy-sdk';
2
3const alchemy = new Alchemy();
4
5alchemy.core.getTokenBalances('vitalik.eth').then(
6 console.log
7);

Questions & Feedback

We’d love your thoughts on what would improve your web3 dev process the most! If you have 5 minutes, tell us what you want at our Feature Request feedback form and we’d love to build it for you:

Alchemy SDK Feedback Form

If you have any questions, issues, or feedback, please file an issue on GitHub, or drop us a message on our Discord channel for the SDK.

This guide provides you with the code examples to get started with all of the Alchemy SDK. Check out our Web3 Tutorials Overview to learn more about building with Alchemy.