Get started building on Flow and using the JSON-RPC API
To use the Flow API you'll need to create a free Alchemy account first!
Introduction
Flow is a blockchain designed for the next generation of apps, games, and digital assets. Known for its high performance, scalability, and developer-friendly features, Flow offers a robust environment for deploying decentralized applications (dApps).
What is the Flow API?
The Flow API facilitates interaction with the Flow network through a collection of JSON-RPC methods. Given its developer-friendly design, those familiar with Ethereum's JSON-RPC APIs will find working with Flow intuitive and straightforward.
Getting Started Instructions
1. Choose a Package Manager (npm or yarn)
Your first step involves selecting a package manager, which will be crucial for managing your project's dependencies. The choice between npm
and yarn
depends on your personal preference or project requirements.
npm | yarn |
---|---|
Begin with npm by following the npm documentation. | For yarn , refer to yarn's installation guide. |
2. Set Up Your Project
To kickstart your project, open your terminal and execute the following commands:
mkdir flow-api-quickstart
cd flow-api-quickstart
npm init --yes
mkdir flow-api-quickstart
cd flow-api-quickstart
yarn init --yes
This creates a new directory named flow-api-quickstart
and initializes a Node.js project within it.
3. Make Your First Request
For making API requests, we'll use Axios, a widely-used HTTP client. Install Axios with the following command:
npm install axios
# Or with yarn
# yarn add axios
Next, create an index.js
file in your project directory. Paste the following code to send a request to the Flow network:
const axios = require('axios');
const url = `https://flow-testnet.g.alchemy.com/v2/${yourAPIKey}`;
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);
});
Remember to replace yourAPIKey
with your actual Alchemy API key that you can get from your Alchemy dashboard.
4. Run Your Script
To execute your script and make a request to the Flow network, run:
node index.js
You should see the latest block information from Flow's blockchain outputted to your console:
Latest Block: { ... }
Next Steps
Well done! You've just made your first request to the Flow API. With this foundation, you can dive deeper into the array of JSON-RPC methods available on Flow and start building your dApps on it!