> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstack.com/llms.txt
> Use this file to discover all available pages before exploring further.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.chainstack.com/feedback

```json
{
  "path": "/reference/getcode",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# eth_getCode | Polygon

Polygon API method that retrieves the compiled bytecode of a smart contract providing its address as a parameter. This method returns a hexadecimal string representing the smart contract's bytecode.

Developers can use this bytecode to verify if a smart contract is legitimate and to ensure that it performs the intended functions.

<Note>
  When called against a block older than the latest \~128 blocks, this method is treated as an archive request (2 RUs instead of 1 RU). See [request units](/docs/request-units#archive-state-methods).
</Note>

<Check>
  **Get your own node endpoint today**

  [Start for free](https://console.chainstack.com/) and get your app to production levels immediately. No credit card required.

  You can sign up with your GitHub, X, Google, or Microsoft account.
</Check>

## Parameters

* `address` — the address of the smart contract to query
* `quantity or tag` — the integer of a block encoded as hexadecimal or the string with:

  * `latest` — the most recent block in the blockchain and the current state of the blockchain at the most recent block
  * `earliest` — the earliest available or genesis block
  * `pending` — the pending state and transactions block. The current state of transactions that have been broadcast to the network but have not yet been included in a block.

<Note>
  See the [default block parameter](https://ethereum.org/en/developers/docs/apis/json-rpc/#default-block).
</Note>

## Response

`data` — the compiled bytecode of a smart contract. Returns `0x` if the address is not associated with a smart contract.

## `eth_getCode` code examples

The following example retrieves the compiled bytecode from the [LINK token smart contract](https://polygonscan.com/token/0xb0897686c545045afc77cf20ec7a532e3120e0f1#code).

<CodeGroup>
  ```javascript web3.js theme={"system"}
  const Web3 = require("web3");
  const NODE_URL = "CHAINSTACK_NODE_URL";
  const web3 = new Web3(NODE_URL);

  async function getCode(address, block) {
    const code = await web3.eth.getCode(address, block)
    console.log(code)
   }
   
   getCode("0xb0897686c545045aFc77CF20eC7A532E3120E0F1", "latest" )
  ```

  ```javascript ethers.js theme={"system"}
  const ethers = require('ethers');
  const NODE_URL = "CHAINSTACK_NODE_URL";
  const provider = new ethers.JsonRpcProvider(NODE_URL);

  const getCode = async (address, block) => {
      const code = await provider.send("eth_getCode", [address, block]);
      console.log(code);
    };

    getCode("0xb0897686c545045aFc77CF20eC7A532E3120E0F1", "latest")
  ```

  ```python web3.py theme={"system"}
  from web3 import Web3  
  node_url = "CHAINSTACK_NODE_URL" 

  code = web3.eth.get_code("0xb0897686c545045aFc77CF20eC7A532E3120E0F1", "latest") 
  print(web3.to_hex(code))   # Convert the Bytes result into HEX.
  ```
</CodeGroup>

## Use case

A practical use case for the `eth_getcode` method is verifying whether an address is associated with a smart contract. This can be particularly useful for blockchain explorers, auditors, and DApp developers, who must ensure that the code they interact with is legitimate.

Here is an example using web3.js:

```javascript index.js theme={"system"}
const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function verifyContractAddress(address) {
  try {
    const bytecode = await web3.eth.getCode(address);
    return bytecode !== '0x';
  } catch (error) {
    console.error(`Error: ${error.message}`);
    return false;
  }
}

async function main() {
  const addressToVerify = "0xc094fd7ef80be094f3d98e227a487490ab1f9b9c"
  const isContract = await verifyContractAddress(addressToVerify);

  if (isContract) {
    console.log(`📑The address ${addressToVerify} is a smart contract.`);
  } else {
    console.log(`🚨The address ${addressToVerify} is not a smart contract.`);
  }
}

main();
```

The `verifyContractAddress` function takes an `address` as a parameter and returns a `boolean` value indicating whether the address is associated with a smart contract. The function works by using the `web3.eth.getCode` method to retrieve the bytecode of the contract associated with the given address. If the returned bytecode is not equal to `0x`, then the function assumes that the address is associated with a smart contract and returns `true`. If an error occurs while calling `web3.eth.getCode`, the function returns `false` and logs the error message to the console.

The `main` function is responsible for calling `verifyContractAddress` with a specific address to verify and log the results to the console. In this example, `main` is set up to verify the address `0xc094fd7ef80be094f3d98e227a487490ab1f9b9c`. After calling `verifyContractAddress`, `main` logs either a success message indicating that the address is associated with a smart contract or a warning message indicating that it is not.

Overall, this code provides a useful utility for verifying the legitimacy of smart contract addresses on the Polygon blockchain. It can be useful for auditors, blockchain explorers, and DApp developers who want to ensure that they are interacting with trustworthy code on the blockchain.


## OpenAPI

````yaml /openapi/polygon_node_api/account_info/eth_getCode.json POST /a9bca2f0f84b54086ceebe590316fff3
openapi: 3.0.0
info:
  title: eth_getCode example
  version: 1.0.0
  description: This is an API example for eth_getCode.
servers:
  - url: https://nd-828-700-214.p2pify.com
security: []
paths:
  /a9bca2f0f84b54086ceebe590316fff3:
    post:
      tags:
        - upload
      summary: eth_getCode
      operationId: getCode
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                id:
                  type: integer
                  default: 1
                jsonrpc:
                  type: string
                  default: '2.0'
                method:
                  type: string
                  default: eth_getCode
                params:
                  type: array
                  items:
                    anyOf:
                      - type: string
                        default: '0xb0897686c545045aFc77CF20eC7A532E3120E0F1'
                      - type: string
                        default: latest
                  default:
                    - '0xb0897686c545045aFc77CF20eC7A532E3120E0F1'
                    - latest
      responses:
        '200':
          description: The code information
          content:
            application/json:
              schema:
                type: object
                properties:
                  jsonrpc:
                    type: string
                  id:
                    type: integer
                  result:
                    type: object

````