Base API Quickstart

Get started building on Base and using the JSON-RPC API

To use the Base API you'll need to create a free Alchemy account first!

Introduction

Base offers a secure, affordable, and developer-centric Ethereum L2 solution. Designed with the vision of making blockchain more accessible, Base emerges from Coinbase's incubation, with clear intentions to move towards increased decentralization in the future. Its goal is to establish an inclusive, global cryptoeconomy. Notably, Base is developed with Optimism, leveraging the MIT-licensed OP Stack. This collaboration higlights Base's commitment to open-source principles and being in sync with the developer community's needs and aspirations.


What is Base API?

The Base API is a collection of JSON-RPC methods that enable developers to interact with the Base network. Notably, it shares the same set of JSON-RPC methods as Optimism. For those already acquainted with Optimism's API, diving into Base will feel both familiar and effortless.

Getting Started Instructions

1. Choose a Package Manager (npm or yarn)

Before you begin, you'll need to choose a package manager to manage dependencies in your project. The two most popular package managers for Node.js are npm and yarn. You can choose the one you're most comfortable with.

npmyarn
To get started with npm, follow the documentation to install Node.js and npm for your operating system.To get started with yarn, follow these steps.

2. Set up your project

Let's start by setting up a simple Node.js project. Open your terminal and run the following commands:

mkdir alchemy-base-api
cd alchemy-base-api
npm init --yes
mkdir alchemy-base-api
cd alchemy-base-api
yarn init --yes

This will create a new directory called alchemy-base-api and initialize a new Node.js project in it.

3. Make Your First Request

To make requests to the Base API, you'll need to use an HTTP client. In this guide, we'll use Axios, a popular HTTP client for Node.js. Install Axios as a dependency in your project with the command below:

npm install axios
yarn add axios

Next, let's create a script that will make a request to the eth_blockNumber JSON-RPC method on Base. Create a new file called index.js in your project directory and add the following code:

const axios = require('axios');

const apiKey = 'YOUR_API_KEY'; // Replace with your Alchemy API key
const url = `https://base-mainnet.g.alchemy.com/v2/${apiKey}`;

const payload = {
  jsonrpc: '2.0',
  id: 1,
  method: 'eth_blockNumber',
  params: []
};

axios.post(url, payload)
  .then(response => {
    console.log('Block Number:', response.data.result);
  })
  .catch(error => {
    console.error(error);
  });

Remember to replace YOUR_API_KEY with your actual Alchemy API key that you can get from your Alchemy dashboard.

4. Run Script

To run the script and make request to the Base API, execute the following command in your terminal:

node index.js

As a result, should see the current block number on Base (in hex format) printed to the console:

Block Number: 0x6d68e

Next Steps

Congratulations! You've successfully made your first request to the Base API using Alchemy. From here, you can explore the various JSON-RPC methods available on Base and start building your decentralized applications on it!

ReadMe