To use the Injective API, you need an Alchemy account. Create a free account to get started.
Injective is a high-performance Layer 1 blockchain built on the Cosmos SDK, purpose-built for decentralized finance (DeFi) applications such as decentralized exchanges, prediction markets, and lending protocols. It features full EVM compatibility, allowing developers to deploy Ethereum-based smart contracts while benefiting from fast finality and low transaction costs.
The Injective API lets you interact with the Injective network through a set of JSON-RPC methods. If you've worked with Ethereum's JSON-RPC APIs, the interface will be familiar.
Pick a package manager for your project's dependencies.
# Begin with npm by following the npm documentation
# https://docs.npmjs.com/downloading-and-installing-node-js-and-npmRun the following commands to create and initialize your project:
mkdir injective-api-quickstart
cd injective-api-quickstart
npm init --yesThis creates a new directory named injective-api-quickstart and initializes a Node.js project within it.
Install Axios to make API requests:
npm install axiosCreate an index.js file in your project directory and paste the following code:
const axios = require('axios');
const url = "https://injective-mainnet.g.alchemy.com/v2/YOUR_API_KEY";
const payload = {
jsonrpc: '2.0',
id: 1,
method: 'eth_blockNumber',
params: []
};
axios.post(url, payload)
.then(response => {
console.log('Latest Block:', response.data.result);
})
.catch(error => {
console.error(error);
});Replace your-api-key with your actual Alchemy API key from the Alchemy Dashboard.
Run your script to make a request to the Injective network:
node index.jsYou should see the latest block number from Injective in your console:
Latest Block: 0x...You've made your first request to the Injective network. Explore the JSON-RPC methods available on Injective and start building your dApps.