getCode - SDK

Returns the contract code of the provided address at the block. If there is no contract deployed, the result is 0x.

Don’t have an API key?

Start using this method in your app today.


Returns the contract code of the provided address at the block. If there is no contract deployed, the result is 0x.


addressOrNamestringThe address or ENS name of the account to get the code for.
blockTagstringThe optional block number or hash. Defaults to latest if unspecified.

blockTag parameters

  • pending - A sample next block built by the client on top of latest and containing the set of transactions usually taken from local mempool. Intuitively, you can think of these as blocks that have not been mined yet.
  • latest - The most recent block in the canonical chain observed by the client, this block may be re-orged out of the canonical chain even under healthy/normal conditions.
  • safe - The most recent crypto-economically secure block, cannot be re-orged outside of manual intervention driven by community coordination. Intuitively, this block is “unlikely” to be re-orged. Only available on Ethereum Goerli.
  • finalized - The most recent crypto-economically secure block, that has been accepted by >2/3 of validators. Cannot be re-orged outside of manual intervention driven by community coordination. Intuitively, this block is very unlikely to be re-orged. Only available on Ethereum Goerli.
  • earliest - The lowest numbered block the client has available. Intuitively, you can think of this as the first block created.


Promise<string>stringReturns the code.

Example Request and Response

Prerequisite: You will need to install the Alchemy SDK before making requests with it.

The commands for installing it using npm or yarn are given below:

npm install alchemy-sdk
yarn add alchemy-sdk


Here is an example of how to make a getCode request using the Alchemy SDK:

// Imports the Alchemy SDK
const { Alchemy, Network } = require("alchemy-sdk");

// Configures the Alchemy SDK
const config = {
    apiKey: "alchemy-replit", // Replace with your API key
    network: Network.ETH_MAINNET, // Replace with your network

// Creates an Alchemy object instance with the config to use for making requests
const alchemy = new Alchemy(config);

const main = async () => {
    let address = "registrar.firefly.eth"

    //Call the method to retrieve code of address
    let response = await alchemy.core.getCode(address)

    //Logging the response to the console




Code Sandbox

You can test out the getCode method using the code sandbox below:

Use Cases

The everyday use case for the getCode method is to return the contract code of an address on the blockchain. However, here are some other possible use cases for the getCode method:

  • Contract verification: Developers can use the getCode method to verify that the bytecode of a deployed contract matches the expected bytecode. This can be used to ensure that a contract has not been tampered with or to verify that a contract has been deployed correctly.

  • Contract debugging: If a contract is not behaving as expected, developers can use getCode to retrieve the bytecode and examine it for errors or unexpected behavior.

  • Contract analysis: Researchers or security auditors might use getCode to analyze the bytecode of a contract for vulnerabilities or other issues.

  • Contract migration: If a contract needs to be migrated to a new address, developers can use getCode to retrieve the bytecode of the existing contract and then deploy it to the new address.

Overall, getCode is a useful tool for developers who are building on the EVM networks and need to interact with deployed contracts. It can be used for a variety of purposes, from debugging and analysis to migration and verification.

Related Methods

Here are the methods related to getCode:

  • getBlock: Returns the block from the network based on the provided block number or hash.
  • getBlockNumber: Returns the block number of the most recently mined block.