Package: alchemy-sdk
Alchemy SDK for PythonAn Alchemy SDK to use theAlchemy API.It supports the exact same syntax and functionality of the Web3eth,
making it a 1:1 mapping for anyone using the Web3eth. However, it adds a
significant amount of improved functionality on top of Web3, such as easy
access to Alchemy’s Enhanced and NFT APIs, 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 ourFeature Request feedback form, and we'd love to build it for you.The SDK currently supports the following chains:Ethereum: Mainnet, GoerliPolygon: Mainnet, MumbaiOptimism: Mainnet, Goerli, KovanArbitrum: Mainnet, Goerli, RinkebyAstar: MainnetGetting startedUse the package managerpipto install alchemy_sdk.pip3installalchemy-sdkAfter installing the app, you can then import and use the SDK:fromalchemyimportAlchemy,Network# create Alchemy object using your Alchemy api key, default is "demo"api_key="your_api_key"# choose preferred network from Network, default is ETH_MAINNETnetwork=Network.ETH_MAINNET# choose the maximum number of retries to perform, default is 5max_retries=3# create Alchemy objectalchemy=Alchemy(api_key,network,max_retries=max_retries)ℹ️ Creating a unique Alchemy API KeyThe public "demo" API key may be rate limited based on traffic. To create your own API key,sign up for an Alchemy account hereand use the key created on your dashboard for the first app.Using the Alchemy SDKThe Alchemy SDK currently supports 2 different namespaces, including:core: All web3.eth methods and Alchemy Enhanced API methodsnft: All Alchemy NFT API methodsIf you are already using web3.eth, you should be simply able to replace the web3.eth object withalchemy.coreand it should work properly.ℹ️ ENS Name ResolutionThe Alchemy SDK 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).fromalchemyimportAlchemyalchemy=Alchemy()# Access standard Web3 request. Gets latest block hashblock_hash=Alchemy.to_hex(alchemy.core.get_block('latest')['hash'])# Access Alchemy Enhanced API requests. Gets all transaction receipts for a given block hash.alchemy.core.get_transaction_receipts(block_hash=block_hash)# Access the Alchemy NFT API. Gets contract metadata for NFT and gets collection namecontract="0x01234567bac6ff94d7e4f0ee23119cf848f93245"print(alchemy.nft.get_contract_metadata(contract).opensea.collection_name)The Alchemy class also supports static methods from Web3 object that streamline the development process:Encoding, Decoding, Hashing:to_bytes,to_int,to_hex,to_text,to_json,keccakCurrency Utility:to_wei,from_weiAddress Utility:is_address,is_checksum_address,to_checksum_addressAlchemy CoreThe core namespace contains all commonly-used Web3.eth methods.It also includes the majority of Alchemy Enhanced APIs, including:get_token_metadata(): Get the metadata for a token contract address.get_token_balances(): Gets the token balances for an owner given a list of contracts.get_asset_transfers(): Get transactions for specific addresses.get_transaction_receipts(): Gets all transaction receipts for a given block.Alchemy NFT APIThe SDK currently supports the followingNFT APIendpoints
under thealchemy.nftnamespace:get_nft_metadata(): Get the NFT metadata for an NFT contract address and tokenId.get_nft_metada_batch(): Get the NFT metadata for multiple NFT contract addresses/token id pairs.get_contract_metadata(): Get the metadata associated with an NFT contract.get_contracts_for_owner(): Get all NFT contracts that the provided owner address owns.get_nfts_for_owner(): Get NFTs for an owner address.get_nfts_for_contract(): Get all NFTs for a contract address.get_owners_for_nft(): Get all the owners for a given NFT contract address and a particular token ID.get_owners_for_contract(): Get all the owners for a given NFT contract address.get_minted_nfts(): Get all the NFTs minted by the owner address.is_spam_contract(): Check whether the given NFT contract address is a spam contract as defined by Alchemy (see theNFT API FAQ)get_spam_contracts(): Returns a list of all spam contracts marked by Alchemy.refresh_contract(): Enqueues the specified contract address to have all token ids' metadata refreshed.get_floor_price(): Return the floor prices of a NFT contract by marketplace.compute_rarity(): Get the rarity of each attribute of an NFT.PaginationThe Alchemy NFT endpoints return 100 results per page. To get the next page, you can pass in
thepageKeyreturned by the previous call.SDK vs API DifferencesThe 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 referencingCollectionhave been renamed to use the nameContractfor greater accuracy: e.g.get_nfts_for_contract.Some methods have different naming that the REST API counterparts in order to provide a consistent API interface (
e.g.get_nfts_for_owner()isalchemy_getNfts,get_owners_for_nft()isalchemy_getOwnersForToken).SDK standardizes toomit_metadataparameter (vs.withMetadata).Standardization topage_keyparameter for pagination (vs.nextToken/startToken)Emptytoken_urifields are omitted.Token ID is always normalized to an integer string onBaseNftandNft.Some fields omitted in the REST response are included in the SDK response in order to return anNftobject.Some fields in the SDK'sNftobject are named differently than the REST response.Usage ExamplesBelow are a few usage examples.Getting the NFTs owned by an addressfromalchemyimportAlchemyfromalchemy.nftimportNftFiltersalchemy=Alchemy()# Get how many NFTs an address owns.response=alchemy.nft.get_nfts_for_owner('vitalik.eth')print(response['total_count'])# Get all the image urls for all the NFTs an address owns.fornftinresponse['owned_nfts']:print(nft.media)# Filter out spam NFTs.nfts_without_spam=alchemy.nft.get_nfts_for_owner('vitalik.eth',exclude_filters=[NftFilters.SPAM])Getting all the owners of the BAYC NFTfromalchemyimportAlchemyalchemy=Alchemy()# Bored Ape Yacht Club contract address.bayc_address='0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D'# Omit the NFT metadata for smaller payloads.response=alchemy.nft.get_nfts_for_contract(bayc_address,omit_metadata=True,page_size=5)fornftinresponse['nfts']:owners=alchemy.nft.get_owners_for_nft(contract_address=nft.contract.address,token_id=nft.token_id)print(f"owners:{owners}, tokenId:{nft.token_id}")Get all outbound transfers for a provided addressfromalchemyimportAlchemyalchemy=Alchemy()print(alchemy.core.get_token_balances('vitalik.eth'))Questions and FeedbackIf you have any questions, issues, or feedback, please file an issue
onGitHub, or drop us a message on
ourDiscordchannel for the SDK.LicenseMIT