---
title: "What is an ABI of a smart contract?"
description: "Examples and Usage for Developers"
---

# What is an ABI of a smart contract?

The Application Binary Interface \(ABI\) of a smart contract gives a contract the ability to communicate and interact with external applications and other smart contracts. Receiving data from external sources can be critical for completing the goals of the application and the user.  

In traditional web development, conversations about data happen between applications and servers through API's \(Application Program Interface\). Servers act as centralized sources of information that feed data to application by request. 

On a blockchain, no such centralization of data exists.  Nodes essentially act as servers and smart contracts are on chain "hosted" functions.  Applications outside of the blockchain \(and other smart contracts\) need a way to communicate with smart contracts that are on-chain. This is where ABI comes into play. 

## **Why ABI?**

Before going into more details about what ABI is, it is good to understand why we have it.

Smart contracts are the core applications of the [EVM \(Ethereum Virtual Machine\)](https://www.alchemy.com/overviews/what-is-the-ethereum-virtual-machine-evm). The purpose of smart contracts is to execute transactions when certain conditions defined in the contract are met. These conditions can be events both on or off-chain. Smart contracts are written in high-level languages like [Solidity](https://www.alchemy.com/overviews/solidity) but they are stored on the EVM as executable bytecode, which is in binary format. 

<ImageBlock
  src="https://media.alchemy.com/1703767018-evm-executable-bytecode.jpeg"
  alt="Diagram of smart contract code stored on the EVM as executable bytecode"
  width={1600}
  height={916}
  caption="EVM as executable bytecode"
/>

Since this bytecode is not human readable, it requires interpretation to be understood. ABI allows anyone writing a smart contract to be able to communicate between a web application written in a high-level language like Javascript and the bytecode that the EVM understands. 

## **What is an ABI?**

Like its Web2 cousin, the API, ABI acts as a function selector, defining the specific methods that can be called to a smart contract for execution. These specific methods and their connected data types are listed in a generated JSON RPC file. 

<ImageBlock
  src="https://media.alchemy.com/1703767166-abi-function-selector.png"
  alt="Diagram of an ABI acting as a function selector for smart contract methods"
  width={760}
  height={484}
  caption="ABI function selector"
/>

Unlike an API, we can't just send a request directly in JSON format to a smart contract and expect a response since a contract only communicates in bytecode. To translate this into something the EVM understands, this information is encoded via ABI encoding.  These encodings include function signatures and variable declarations so that the EVM knows exactly which function to execute within the smart contract. 

<ImageBlock
  src="https://media.alchemy.com/1703767249-abi-encoding.png"
  alt="Example of ABI encoding translating a function call into EVM bytecode"
  width={1362}
  height={280}
  caption="ABI encoding"
/>

The responses are also in the bytecode so interpretation is required before it is processed by a web application. The advantage of using bytecode in the response is that we can also expect a certain structure to be returned after calling a contract's function. 

## **How to use ABI?**

### Generation

If you are using tooling like Hardhart/Truffle or an IDE like [Remix](https://remix.ethereum.org/#optimize=false&runs=200&evmVersion=null&version=soljson-v0.8.7+commit.e28d00a7.js), the contract ABI is automatically generated for you. You can also manually create the ABI by using the [Solidity Compiler NPM package](https://www.npmjs.com/package/solc). After installing the package, you can run the ‘`solcjs contractname.sol --abi`’ command in a terminal. This will generate an .abi file if performed successfully. 

Now that you have a generated ABI, let's look at some of the elements in this file:  

<ImageBlock
  src="https://media.alchemy.com/1703767374-generated-contract-abi.png"
  alt="Example of a generated smart contract ABI JSON file"
  width={1600}
  height={730}
  caption="Generated contract ABI"
/>

### **Executing**

As ABI operates as the interpreter between the EVM bytecode and Javascript of a website,  it is needed when you want to execute any functions of a smart contract. In addition to the ABI, the contract's address on the blockchain is required. Here is a small Javascript code snippet to show how this is done:

<ImageBlock
  src="https://media.alchemy.com/1703767477-executing-abi.png"
  alt="JavaScript snippet executing a contract function using its ABI and address"
  width={1600}
  height={880}
  caption="Executing ABI"
/>

If you are interested in finding the ABI of an already deployed contract, you can find this by searching on [Etherscan](https://www.alchemy.com/dapps/etherscan) with the contract's address. For example [here](https://etherscan.io/address/0xb4eaf48bd7f72356e1019c157e91b81a1c541073#code):

<ImageBlock
  src="https://media.alchemy.com/1703767569-abi-of-already-deployed-contract.png"
  alt="Etherscan contract page showing the ABI of an already deployed contract"
  width={1600}
  height={315}
  caption="ABI of an already deployed contract"
/>

### Encoding



Since all communication is done in bytecode, it would be difficult to expect developers to encode these messages themselves. Fortunately, popular compilers like Remix can also handle the encoding for you. These encodings follow a certain pattern, so one can have a better idea of what is going on by reviewing the [ABI Specification](https://docs.soliditylang.org/en/v0.8.11/abi-spec.html).

The first four bytes are the function signature which indicates what type of function in the smart contract is being executed. A popular function identifier is a9059cbb which indicates that this is an [ERC20](https://www.alchemy.com/overviews/erc20-solidity) transfer. There is a [database directory](https://www.4byte.directory/) of function signatures here where you can explore more. 

<ImageBlock
  src="https://media.alchemy.com/1703767678-function-hashes.png"
  alt="Examples of ABI function signature hashes, including the ERC20 transfer selector"
  width={1069}
  height={418}
  caption="ABI function hashes"
/>

From the fifth byte onward are where the arguments are encoded. Responses follow this similar structure but without the function signature included. 

## **Conclusion**

ABI can oftentimes be an overlooked aspect of working with smart contracts but it plays an important role in the usability of this technology.  Building on [smart contract tutorials](https://www.alchemy.com/docs/how-to-deploy-a-smart-contract-to-the-sepolia-testnet) is a great way to understand the power of this silent workhorse and a great way to apply your knowledge.
