To use the Starknet API you'll need to create a free Alchemy account first!
Starknet is a decentralized Validity-Rollup (also known as ZK-Rollup) that operates as a Layer 2 network over Ethereum, enabling apps to achieve massive scale without compromising Ethereum's composability and security.
The Starknet API is a collection of JSON-RPC methods that let you interact with the Starknet network. You can access up-to-date network data and submit transactions through the provided endpoints.
Choose a package manager to manage your project's dependencies. The two most popular options for Node.js are npm and yarn.
npm
Install Node.js and npm for your operating system by following the npm documentation.
yarn
Install yarn by following the yarn installation guide.
Set up a Node.js project. Open your terminal and run the following commands:
mkdir alchemy-starknet-api
cd alchemy-starknet-api
npm init --yesThis will create a new directory called alchemy-starknet-api and initialize a new Node.js project in it.
You need an HTTP client to make requests to the Starknet API. Install Axios with the command below:
npm install axiosCreate a file called index.js in your project directory and add the following code to request the starknet_blockNumber JSON-RPC method:
const axios = require('axios');
const apiKey = 'YOUR_API_KEY'; // Replace with your Alchemy API key
const url = `https://starknet-mainnet.g.alchemy.com/v2/${apiKey}`;
const payload = {
jsonrpc: '2.0',
id: 1,
method: 'starknet_blockNumber',
params: []
};
axios.post(url, payload)
.then(response => {
console.log('Block Number:', response.data.result);
})
.catch(error => {
console.error(error);
});Replace YOUR_API_KEY with your actual Alchemy API key from the Alchemy Dashboard.
Run the following command in your terminal:
node index.jsYou should see the current block number on Starknet printed to the console:
Block Number: 42390You've made your first request to the Starknet API using Alchemy. From here, you can explore the JSON-RPC methods available on Starknet and start building your decentralized applications.