Gathering detailed insights and metrics for alchemy-sdk
Gathering detailed insights and metrics for alchemy-sdk
Gathering detailed insights and metrics for alchemy-sdk
Gathering detailed insights and metrics for alchemy-sdk
The easiest way to connect your dApp to the blockchain.
npm install alchemy-sdk
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
404 Stars
413 Commits
184 Forks
31 Watching
20 Branches
34 Contributors
Updated on 22 Nov 2024
Minified
Minified + Gzipped
TypeScript (99.39%)
JavaScript (0.6%)
Shell (0.01%)
Cumulative downloads
Total Downloads
Last day
2%
10,555
Compared to previous day
Last week
4%
54,935
Compared to previous week
Last month
4.2%
233,977
Compared to previous month
Last year
10.7%
2,738,619
Compared to previous year
14
26
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 (chains with '(d)' after are deprecated):
You can find per-method documentation of the Alchemy SDK endpoints at the Alchemy Docs linked in the sidebar.
npm install alchemy-sdk
After installing the app, you can then import and use the SDK:
1import { Alchemy, Network } from 'alchemy-sdk'; 2 3// Optional config object, but defaults to the API key 'demo' and Network '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);
ℹ️ 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.
The Alchemy SDK currently supports the following namespaces:
core
: All commonly-used Ethers.js Provider methods and Alchemy Enhanced API methodsnft
: All Alchemy NFT API methodsws
: All WebSockets methodstransact
: All Alchemy Transaction API methodsnotify
: CRUD endpoints for modifying Alchemy Notify Webhooksdebug
: Methods to inspect and replay transactions and blocksIf 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
).
1import { Alchemy, AlchemySubscription } from 'alchemy-sdk'; 2 3// Using default settings - pass in a settings object to specify your API key and network 4const alchemy = new Alchemy(); 5 6// Access standard Ethers.js JSON-RPC node request 7alchemy.core.getBlockNumber().then(console.log); 8 9// Access Alchemy Enhanced API requests 10alchemy.core 11 .getTokenBalances('0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE') 12 .then(console.log); 13 14// Access the Alchemy NFT API 15alchemy.nft.getNftsForOwner('vitalik.eth').then(console.log); 16 17// Access WebSockets and Alchemy-specific WS methods 18alchemy.ws.on( 19 { 20 method: AlchemySubscription.PENDING_TRANSACTIONS 21 }, 22 res => console.log(res) 23);
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.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.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 addressTo 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.
1import { Alchemy } from 'alchemy-sdk'; 2 3const alchemy = new Alchemy(); 4 5async function runAlchemy() { 6 const ethersProvider = await alchemy.config.getProvider(); 7 console.log(ethersProvider.formatter); 8} 9runAlchemy();
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:
1import { Alchemy, AlchemySubscription } from 'alchemy-sdk'; 2 3const alchemy = new Alchemy(); 4 5// Listen to all new pending transactions. 6alchemy.ws.on('block', res => console.log(res)); 7 8// Listen to only the next transaction on the USDC contract. 9alchemy.ws.once( 10 { 11 method: AlchemySubscription.PENDING_TRANSACTIONS, 12 toAddress: 'vitalik.eth' 13 }, 14 res => console.log(res) 15); 16 17// Remove all listeners. 18alchemy.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:
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.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 contractgetContractMetadataBatch()
: 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.reportSpam()
: Report feedback that a given NFT contract address is a spam contract as defined by Alchemy.isAirdropNft()
: Check whether the given NFT token is marked as an airdrop or not. Airdrops are defined as NFTs that were minted to a user address in a transaction sent by a different address.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.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:
1import { Alchemy } from 'alchemy-sdk'; 2 3const alchemy = new Alchemy(); 4 5async function main() { 6 const ownerAddress = 'vitalik.eth'; 7 for await (const nft of alchemy.nft.getNftsForOwnerIterator(ownerAddress)) { 8 console.log('ownedNft:', nft); 9 } 10} 11 12main();
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:
Collection
have been renamed to use the name Contract
for greater accuracy: e.g. getNftsForContract
.getNftsForOwner()
is alchemy_getNfts
, getOwnersForNft()
is alchemy_getOwnersForToken
).omitMetadata
parameter (vs. withMetadata
).pageKey
parameter for pagination (vs. nextToken
/startToken
)TokenUri
fields are omitted.BaseNft
and Nft
.Nft
object.Nft
object are named differently than the REST response.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.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.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.
Below are a few usage examples.
**ℹ️ More Examples **
You can also go here: Examples Using the Alchemy SDK.
1import { Alchemy, NftExcludeFilters } from 'alchemy-sdk'; 2 3const alchemy = new Alchemy(); 4 5// Get how many NFTs an address owns. 6alchemy.nft.getNftsForOwner('vitalik.eth').then(nfts => { 7 console.log(nfts.totalCount); 8}); 9 10// Get all the image urls for all the NFTs an address owns. 11async function main() { 12 for await (const nft of alchemy.nft.getNftsForOwnerIterator('vitalik.eth')) { 13 console.log(nft.media); 14 } 15} 16 17main(); 18 19// Filter out spam NFTs. 20alchemy.nft 21 .getNftsForOwner('vitalik.eth', { 22 excludeFilters: [NftExcludeFilters.SPAM] 23 }) 24 .then(console.log);
1import { Alchemy } from 'alchemy-sdk'; 2 3const alchemy = new Alchemy(); 4 5// Bored Ape Yacht Club contract address. 6const baycAddress = '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D'; 7 8async function main() { 9 for await (const nft of alchemy.nft.getNftsForContractIterator(baycAddress, { 10 // Omit the NFT metadata for smaller payloads. 11 omitMetadata: true 12 })) { 13 await alchemy.nft 14 .getOwnersForNft(nft.contract.address, nft.tokenId) 15 .then(response => 16 console.log('owners:', response.owners, 'tokenId:', nft.tokenId) 17 ); 18 } 19} 20 21main();
1import { Alchemy } from 'alchemy-sdk'; 2 3const alchemy = new Alchemy(); 4 5alchemy.core.getTokenBalances('vitalik.eth').then(console.log);
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.
No vulnerabilities found.
No security vulnerabilities found.
@alch/alchemy-web3
Web3 client extended with Alchemy and browser provider integration.
@balancer-labs/sdk
JavaScript SDK for interacting with the Balancer Protocol V2
@alchemy/aa-signers
A collection of signers used to create ERC-4337 smart accounts
@alchemy/aa-alchemy
adapters for @alchemy/aa-core for interacting with alchemy services