• Stars
    star
    364
  • Rank 117,101 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The easiest way to connect your dApp to the blockchain.

Alchemy SDK for Javascript

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.

๐Ÿ™‹โ€โ™€๏ธ FEATURE REQUESTS:

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 on our Feature Request feedback form, and we'd love to build it for you.

The SDK currently supports the following chains:

  • Ethereum: Mainnet, Goerli, Sepolia
  • Polygon: Mainnet, Mumbai
  • Optimism: Mainnet, Goerli, Kovan
  • Arbitrum: Mainnet, Goerli, Rinkeby
  • Astar: Mainnet
  • PolygonZKEVM: Mainnet, Testnet

You can find per-method documentation of the Alchemy SDK endpoints at the Alchemy Docs linked in the sidebar.

Getting started

npm install alchemy-sdk

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

import { Alchemy, Network } from 'alchemy-sdk';

// Optional config object, but defaults to the API key 'demo' and Network 'eth-mainnet'.
const settings = {
  apiKey: 'demo', // Replace with your Alchemy API key.
  network: Network.ETH_MAINNET // Replace with your network.
};

const alchemy = new Alchemy(settings);

โ„น๏ธ Creating a unique Alchemy API Key

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.

Using the Alchemy SDK

The Alchemy SDK currently supports the following namespaces:

  • 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
  • debug: Methods to inspect and replay transactions and blocks

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 work properly.

โ„น๏ธ ENS Name Resolution

The Alchemy SDK now supports ENS names (e.g. vitalik.eth) for every parameter where you can pass in a Externally Owned Address, or user address (e.g. 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045).

import { Alchemy, AlchemySubscription } from 'alchemy-sdk';

// Using default settings - pass in a settings object to specify your API key and network
const alchemy = new Alchemy();

// Access standard Ethers.js JSON-RPC node request
alchemy.core.getBlockNumber().then(console.log);

// Access Alchemy Enhanced API requests
alchemy.core
  .getTokenBalances('0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE')
  .then(console.log);

// Access the Alchemy NFT API
alchemy.nft.getNftsForOwner('vitalik.eth').then(console.log);

// Access WebSockets and Alchemy-specific WS methods
alchemy.ws.on(
  {
    method: AlchemySubscription.PENDING_TRANSACTIONS
  },
  res => console.log(res)
);

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.
    • Interface: Found in Utils.Interface, this class abstracts the encoding and decoding required to interact with contracts on the Ethereum network.
  • 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.

Alchemy Settings

An AlchemySettings object can be passed on instantiation to the Alchemy object, with the following optional parameters:

  • apiKey: API key that can be found in the Alchemy dashboard. Defaults to demo: a rate-limited public key.
  • network: Name of the network. Defaults to Network.ETH_MAINNET
  • maxRetries: The maximum number of retries to attempt if a request fails. Defaults to 5.
  • url: Optional URL endpoint to use for all requests. Setting this field will override the URL generated by the network andapiKey fields.
  • authToken: Alchemy auth token required to use the Notify API. This token can be found in the Alchemy Dashboard on the Notify tab.
  • batchRequests: Optional setting that automatically batches and sends json-rpc requests for higher throughput and reduced network IO. Defaults to false.
  • requestTimeout: Optional setting that sets the timeout for requests in milliseconds for the NFT and Notify namespaces. Defaults to no timeout.

Alchemy Core

The core namespace contains all commonly-used Ethers.js Provider methods. If you are already using Ethers.js, you should be simply able to replace the Ethers.js Provider object with alchemy.core when accessing provider methods and it should just work.

It also includes the majority of Alchemy Enhanced APIs, including:

  • getTokenMetadata(): Get the metadata for a token contract address.
  • getTokenBalances(): Gets the token balances for an owner given a list of contracts.
  • getAssetTransfers(): Get transactions for specific addresses.
  • getTransactionReceipts(): Gets all transaction receipts for a given block.

You will also find the following utility methods:

  • findContractDeployer(): Find the contract deployer and block number for a given contract address.
  • getTokensForOwner(): Get all token balances and metadata for a given owner address

Accessing the full Ethers.js Provider

To keep the package clean, we don't support certain uncommonly-used Ethers.js Provider methods as top-level methods in the Alchemy core namespace - for example, provider.formatter. If you'd like to access these methods, simply use the alchemy.config.getProvider() function to configure the Ethers.js Provider AlchemyProvider and return it.

import { Alchemy } from 'alchemy-sdk';

const alchemy = new Alchemy();

async function runAlchemy() {
  const ethersProvider = await alchemy.config.getProvider();
  console.log(ethersProvider.formatter);
}
runAlchemy();

Alchemy WebSockets

In addition to the built-in Ethers.js listeners, the Alchemy SDK includes support for Alchemy's Subscription API. This allows you to subscribe to events and receive updates as they occur.

The alchemy.ws instance can be used like the standard Ethers.js WebSocketProvider to add listeners for Alchemy events:

import { Alchemy, AlchemySubscription } from 'alchemy-sdk';

const alchemy = new Alchemy();

// Listen to all new pending transactions.
alchemy.ws.on('block', res => console.log(res));

// Listen to only the next transaction on the USDC contract.
alchemy.ws.once(
  {
    method: AlchemySubscription.PENDING_TRANSACTIONS,
    toAddress: 'vitalik.eth'
  },
  res => console.log(res)
);

// Remove all listeners.
alchemy.ws.removeAllListeners();

The SDK brings multiple improvements to ensure correct WebSocket behavior in cases of temporary network failure or dropped connections. As with any network connection, you should not assume that a WebSocket will remain open forever without interruption, but correctly handling dropped connections and reconnection by hand can be challenging to get right. The Alchemy SDK automatically handles these failures with no configuration necessary. The main benefits are:

  • Resilient event delivery: Unlike standard Web3.js or Ethers.js, you will not permanently miss events which arrive while the backing WebSocket is temporarily down. Instead, you will receive these events as soon as the connection is reopened. Note that if the connection is down for more than 120 blocks (approximately 20 minutes), you may still miss some events that were not part of the most recent 120 blocks.
  • Lowered rate of failure: Compared to standard Web3.js or Ethers.js, there are fewer failures when sending requests over the WebSocket while the connection is down. The Alchemy SDK will attempt to send the requests once the connection is reopened. Note that it is still possible, with a lower likelihood, for outgoing requests to be lost, so you should still have error handling as with any network request.

Alchemy Transact

The transact namespace contains methods used for simulating and sending transactions. The unique methods to the transact namespace are:

  • sendPrivateTransaction(): Send a private transaction through Flashbots.
  • cancelPrivateTransaction(): Cancel a private transaction sent with Flashbots.
  • simulateAssetChanges(): Simulate a transaction and get a list of asset changes.
  • simulateExecution(): Simulate a transaction and get a full list of internal transactions, logs, ABI decoded results and more.
  • simulateAssetChangesBundle(): Simulate a list of transactions in sequence and get a list of asset changes.
  • simulateExecutionBundle(): Simulate a list of transactions in sequence and get a full list of internal transactions, logs, ABI decoded results and more.

The transact namespace also aliases over several commonly used methods from the core namespace for convenience:

  • getTransaction(): Returns the transaction for the given transaction hash.
  • sendTransaction(): Sends a standard transaction to the network to be mined.
  • waitForTransaction(): Waits for a transaction to be mined and returns the transaction receipt.

Alchemy NFT API

The SDK currently supports the following NFT API endpoints under the alchemy.nft namespace:

  • getNftMetadata(): Get the NFT metadata for an NFT contract address and tokenId.
  • getNftMetadataBatch(): Get the NFT metadata for multiple NFT contract addresses/token id pairs.
  • getContractMetadata(): Get the metadata associated with an NFT contract
  • getContractMetadataBatch(): Get the metadata associated with multiple NFT contracts in a single request.
  • getContractsForOwner(): Get all NFT contracts that the provided owner address owns.
  • getNftsForOwner(): Get NFTs for an owner address.
  • getNftsForOwnerIterator(): Get NFTs for an owner address as an async iterator (handles paging automatically).
  • getNftsForContract(): Get all NFTs for a contract address.
  • getNftsForContractIterator(): Get all NFTs for a contract address as an async iterator (handles paging automatically).
  • getOwnersForNft(): Get all the owners for a given NFT contract address and a particular token ID.
  • getOwnersForContract(): Get all the owners for a given NFT contract address.
  • getMintedNfts(): Get all the NFTs minted by the owner address.
  • getTransfersForOwner(): Get all the NFT transfers for a given owner address.
  • getTransfersForContract(): Get all the NFT transfers for a given NFT contract address.
  • verifyNftOwnership(): Check whether the provided owner address owns the provided NFT contract addresses.
  • isSpamContract(): Check whether the given NFT contract address is a spam contract as defined by Alchemy (see the NFT API FAQ)
  • getSpamContracts(): Returns a list of all spam contracts marked by Alchemy.
  • refreshNftMetadata(): Refresh the cached NFT metadata for a contract address and a single tokenId.
  • refreshContract(): Enqueues the specified contract address to have all token ids' metadata refreshed.
  • getFloorPrice(): Return the floor prices of a NFT contract by marketplace.
  • computeRarity(): Get the rarity of each attribute of an NFT.
  • getNftSales(): Returns NFT sales that have happened through on-chain marketplaces.
  • summarizeNftAttributes(): Get the summary of attribute prevalence for all NFTs in a contract.
  • searchContractMetadata(): Search for a keyword across metadata of all ERC-721 and ERC-1155 smart contracts.

Pagination

The Alchemy NFT endpoints return 100 results per page. To get the next page, you can pass in the pageKey returned by the previous call. To simplify paginating through all results, the SDK provides the getNftsIterator() and getNftsForContractIterator() functions that automatically paginate through all NFTs and yields them via an AsyncIterable.

Here's an example of how to paginate through all the NFTs in Vitalik's ENS address:

import { Alchemy } from 'alchemy-sdk';

const alchemy = new Alchemy();

async function main() {
  const ownerAddress = 'vitalik.eth';
  for await (const nft of alchemy.nft.getNftsForOwnerIterator(ownerAddress)) {
    console.log('ownedNft:', nft);
  }
}

main();

SDK vs API Differences

The NFT API in the SDK standardizes response types to reduce developer friction, but note this results in some differences compared to the Alchemy REST endpoints:

  • Methods referencing Collection have been renamed to use the name Contract for greater accuracy: e.g. getNftsForContract.
  • Some methods have different naming that the REST API counterparts in order to provide a consistent API interface ( e.g. getNftsForOwner() is alchemy_getNfts, getOwnersForNft() is alchemy_getOwnersForToken).
  • SDK standardizes to omitMetadata parameter (vs. withMetadata).
  • Standardization to pageKey parameter for pagination (vs. nextToken/startToken)
  • Empty TokenUri fields are omitted.
  • Token ID is always normalized to an integer string on BaseNft and Nft.
  • Some fields omitted in the REST response are included in the SDK response in order to return an Nft object.
  • Some fields in the SDK's Nft object are named differently than the REST response.

Alchemy Notify

The Alchemy Notify API helps developers set up webhooks in their apps. The namespace provides methods to programmatically create, read, update, and delete your webhooks along with typings for the different webhooks. To learn more about Webhooks, please refer to the Alchemy documentation.

Methods on the NotifyNamespace can be accessed via alchemy.notify. To use the methods, you must include your team's auth token in the authToken field of AlchemySettings when instantiating the SDK. The auth token can be found on the Alchemy Dashboard in the Notify Tab.

Methods include:

  • getAllWebhooks(): Get all webhooks on your team.
  • getAddresses(): Get all addresses tracked for the provided Address Activity Webhook.
  • getNftFilters(): Get all NFT filters tracked for the provided NFT Activity Webhook.
  • createWebhook(): Create a new webhook.
  • updateWebhook(): Update an existing webhook's active status or tracked addresses and NFT filters.
  • deleteWebhook(): Delete the provided webhook.

Alchemy Debug

Methods on the DebugNamespace can be accessed via alchemy.debug. These methods are used for inspecting and debugging transactions.

Methods include:

  • traceCall(): Run an eth_call with the context of the provided block execution using the final state of the parent block as the base.
  • traceTransaction(): Run the transaction in the exact same manner as it was executed on the network. It will replay any transaction that may have been executed prior to this one before it and will then attempt to execute the transaction that corresponds to the given hash.
  • traceBlock(): Replay a block that has already been mined.

Documentation

The SDK is documented via tsdoc comments in the source code. The generated types and documentation are included when using an IDE. To browse the documentation separately, you can view the generated API interfaces in etc/alchemy-sdk.api.md. You can view generated Markdown files for each endpoint in the docs-md directory, or as a webpage by opening docs/index.html in your browser.

Usage Examples

Below are a few usage examples.

**โ„น๏ธ More Examples **

You can also go here: Examples Using the Alchemy SDK.

Getting the NFTs owned by an address

import { Alchemy, NftExcludeFilters } from 'alchemy-sdk';

const alchemy = new Alchemy();

// Get how many NFTs an address owns.
alchemy.nft.getNftsForOwner('vitalik.eth').then(nfts => {
  console.log(nfts.totalCount);
});

// Get all the image urls for all the NFTs an address owns.
async function main() {
  for await (const nft of alchemy.nft.getNftsForOwnerIterator('vitalik.eth')) {
    console.log(nft.media);
  }
}

main();

// Filter out spam NFTs.
alchemy.nft
  .getNftsForOwner('vitalik.eth', {
    excludeFilters: [NftExcludeFilters.SPAM]
  })
  .then(console.log);

Getting all the owners of the BAYC NFT

import { Alchemy } from 'alchemy-sdk';

const alchemy = new Alchemy();

// Bored Ape Yacht Club contract address.
const baycAddress = '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D';

async function main() {
  for await (const nft of alchemy.nft.getNftsForContractIterator(baycAddress, {
    // Omit the NFT metadata for smaller payloads.
    omitMetadata: true
  })) {
    await alchemy.nft
      .getOwnersForNft(nft.contract.address, nft.tokenId)
      .then(response =>
        console.log('owners:', response.owners, 'tokenId:', nft.tokenId)
      );
  }
}

main();

Get all outbound transfers for a provided address

import { Alchemy } from 'alchemy-sdk';

const alchemy = new Alchemy();

alchemy.core.getTokenBalances('vitalik.eth').then(console.log);

Questions and Feedback

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.

More Repositories

1

create-web3-dapp

The complete toolbox to create web3 applications.
TypeScript
824
star
2

rundler

An ERC-4337 Bundler in Rust
Rust
265
star
3

alchemy-web3

Web3 client extended with Alchemy integration
TypeScript
245
star
4

nft-minter-tutorial

JavaScript
236
star
5

aa-sdk

TypeScript
220
star
6

NFT-Marketplace-Tutorial

NFT marketplace tutorial by Alchemy
JavaScript
201
star
7

learn-solidity-presentations

All of the presentations in the Learn Solidity course
Solidity
198
star
8

Build-Your-NFT-Explorer

A simple demo to show case how Alchemy's NFT API works.
JavaScript
118
star
9

modular-account

Solidity
100
star
10

light-account

Solidity
95
star
11

ecdsa-node

JavaScript
81
star
12

nft-api-javascript-scripts

A collection of Javascript scripts running with Alchemy Web3.js, Fetch, or Axios
JavaScript
60
star
13

RTW3-Week2-BuyMeACoffee-Website

JavaScript
52
star
14

RTW3-Week2-BuyMeACoffee-Contracts

JavaScript
46
star
15

hardhat-ethers-react-ts-starter

A web3 starter project using Typescript, Hardhat, ethers.js and @web3-react
TypeScript
44
star
16

GiftList

Gifts under the Merkle Tree
JavaScript
40
star
17

cw3d-nft-explorer

JavaScript
39
star
18

RTW3-Week7-NFT-Marketplace

Road to Web3 Week7 tutorial on building an NFT Marketplace from Scratch
JavaScript
36
star
19

alchemy-docs

The publicly-accessible documentation for Alchemy.com!
36
star
20

hello-world-part-four-tutorial

JavaScript
36
star
21

web3-starter-projects

Fork a blockchain repo and build your dapp. Fast.
32
star
22

alchemy-flow-contracts

Cadence
31
star
23

alchemy-sdk-py

Python
30
star
24

embedded-accounts-quickstart

An application that demos how to quickly build with Alchemy Embedded Accounts.
TypeScript
28
star
25

hello-world-tutorial

Solidity
25
star
26

eth-provider-benchmark

Compare the accuracy and consistency of Ethereum service providers head to head on a suite of tests.
Python
25
star
27

Contract-Puzzles

JavaScript
20
star
28

solana-nft-tutorial

19
star
29

blockexplorer

JavaScript
19
star
30

escrow-hardhat

JavaScript
18
star
31

Alchemy-Hacker-Handbook

JavaScript
18
star
32

RTW3-Week-4-NFT-Gallery

JavaScript
16
star
33

aa-benchmarks

TypeScript
16
star
34

aa-starter-zksync

TypeScript
15
star
35

Local-Hardhat-Games

Solidity
14
star
36

uniswap-trading-example

TypeScript
14
star
37

Build-Your-NFT-Explorer-walkthrough

JavaScript
13
star
38

webhook-examples

Examples of using Alchemy Notify
Python
13
star
39

polygon-smart-contract-tutorial

A guide for coding and deploying a basic Polygon (MATIC) smart contract!
JavaScript
9
star
40

netlify-alchemy-dapp-boilerplates

JavaScript
8
star
41

create-web3-dapp-examples

JavaScript
8
star
42

docs-openapi-specs

OpenAPI Specs for methods on Alchemy docs
TypeScript
8
star
43

nft-indexer

Display all of an addresses's NFTs using Alchemy's Enhanced APIs + Alchemy SDK!
JavaScript
8
star
44

MintGovernance

JavaScript
7
star
45

nft-api-demo-scripts

Shell
7
star
46

embedded-accounts-demo

Demo for Alchemy Embedded Accounts
TypeScript
7
star
47

modular-account-plugin

A very basic ERC6900 compliant plugin
Solidity
7
star
48

multisig-plugin

Solidity
7
star
49

alchemy-web3-webpack-example

Simple project demonstrating Alchemy-Web3 in a browser
JavaScript
6
star
50

nft-api-example

Repository with some example calls to the Alchemy NFT API
TypeScript
6
star
51

erc20-indexer

Index all of an address's ERC-20 token balances in an instant using Alchemy's Enhanced APIs!
JavaScript
6
star
52

smart-accounts-from-scratch

JavaScript
6
star
53

accountkit-react-native-boilerplate

Alchemy AA-SDK React Native boilerplate with Typescript, Viem, and Wagmi
TypeScript
5
star
54

zksync-paymaster-example

Solidity
5
star
55

MintNFT

Mint Your Own NFT!
JavaScript
5
star
56

nft-explorer-vercel

JavaScript
5
star
57

aa-virtual-cold-storage

Supercharging smart contract account security with custom virtual cold storage plugin functionalities | Built with @alchemyplatform Modular Smart Contract Account (ERC 6900) & aa-sdk | Learn More: https://accountkit.alchemy.com/
Solidity
5
star
58

solana-hello-world

Alchemy's Introductory Tutorial for Solana
TypeScript
4
star
59

polygon-nft-finder

Simple demo for finding NFT drops on Polygon (MATIC) via Alchemy Transfers API
HTML
4
star
60

token-api-javascript-scripts

A collection of Javascript scripts running with Alchemy Web3.js, Fetch, or Axios Topics Resources
JavaScript
4
star
61

Transaction-Lifecycle-via-SMS

Python
4
star
62

transfers_api_javascript_scripts

A collection for scripts for using the Alchemy Transfers API
JavaScript
3
star
63

historical_transactions_polygon_scripts

Script for querying historical transactions on Polygon (MATIC) via Alchemy Transfers API
Python
3
star
64

cw3d-evm-boilerplate

JavaScript
3
star
65

entrypoint-hash-poc

Solidity
3
star
66

viem-speedrun

TypeScript
3
star
67

netlify-alchemy-nft-explorer-template

JavaScript
3
star
68

alchemy-multichain-demo

Demo on how to manage multiple chains when using the Alchemy SDK
TypeScript
2
star
69

get-nfts-script

JavaScript script to get all NFTs owned by a specified address
JavaScript
2
star
70

netlify-alchemy-dapp-boilerplates-w-hardhat

JavaScript
2
star
71

vercel-alchemy-dapp-boilerplates

JavaScript
2
star
72

AWS-serverless-blockchain-data-ingesting-infrastructure

JavaScript
2
star
73

alchemy-chainlink-spring-hackathon-nft

JavaScript
2
star
74

block-race

A simple script for comparing how quickly Eth providers publish new blocks.
TypeScript
2
star
75

alchemy-asset-transfers-benchmark

TypeScript
2
star
76

Alchemy-Notify-Tutorial

Tutorial for integrating transaction activity notifications into your dApp.
JavaScript
2
star
77

spearmint-sdk

An SDK for Alchemy's free and automated web3 allowlist platform.
2
star
78

gasless-minter-tutorial

Tutorial on setting up a full-stack application with account abstraction to enable gasless minting.
TypeScript
2
star
79

aa-simple-dapp

A simple Next.js app allows for gas-less ERC-20 mints using Account Abstraction.
TypeScript
2
star
80

creating-smart-contract-and-sending-userops

TypeScript
2
star
81

aa-sdk-rn-expo

Example repo using aa-sdk with react native and expo
TypeScript
2
star
82

aa-sdk-userops

TypeScript
2
star
83

erc20-example-foundry

A simple Foundry Repo to build, test, and deploy an ERC-20 token.
Solidity
2
star
84

try-alchemy-sdk

super quick repo to get started with the alchemy-sdk
JavaScript
1
star
85

transaction-receipts-scripts

A collection of Ethereum transaction receipt scripts
Python
1
star
86

usdc-token-gate

Demo project for ETH Denver!
JavaScript
1
star
87

gm

JavaScript
1
star
88

Alchemy-Transfers-Tutorial

Tutorial for integrating historical transaction activity into your dApp dashboard.
Python
1
star
89

vercel-alchemy-dapp-boilerplates-w-hardhat

JavaScript
1
star
90

sponsoring-userops

TypeScript
1
star
91

aa-demo

Demo of account abstraction functionality using CW3D + Userbase.
TypeScript
1
star
92

creating-light-smart-account-and-sending-user-ops

TypeScript
1
star
93

token_api_javascript_scripts

Javascript Scripts for Token API tutorials
JavaScript
1
star