How to get started building on Polygon PoS and using the JSON-RPC API
Don’t have an API key?
Sign up to start building on Polygon
Getting Started Instructions
1. Choose a package manager (npm or yarn)
For this guide, we will be using npm or yarn as our package manager to install either alchemy-sdk
or any other packages.
npm
To get started with npm
, follow the documentation to install Node.js and npm
for your operating system: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
yarn
To get started with yarn
, follow these steps: https://classic.yarnpkg.com/lang/en/docs/install
2. Set up your project (npm or yarn)
mkdir alchemy-polygon-api
cd alchemy-polygon-api
npm init --yes
mkdir alchemy-polygon-api
cd alchemy-polygon-api
yarn init --yes
3. Install Alchemy-SDK
Run the following command to install the Alchemy SDK with npm or yarn.
npm install alchemy-sdk
yarn add alchemy-sdk
4. Make your first request
You are all set now to use Polygon PoS API and make your first request. For instance, lets make a request to get latest block
. Create an index.js
file and paste the following code snippet into the file.
const { Network, Alchemy } = require("alchemy-sdk");
// Optional Config object, but defaults to demo api-key and eth-mainnet.
const settings = {
apiKey: "demo", // Replace with your Alchemy API Key.
network: Network.MATIC_MAINNET, // Replace with your network.
};
const alchemy = new Alchemy(settings);
async function main() {
const latestBlock = await alchemy.core.getBlockNumber();
console.log("The latest block number is", latestBlock);
}
main();
5. Run script
To run the above node script, use cmd node index.js
, and you should see the output.
The latest block number is 36250231
Polygon Tutorials
You must not stop here! Want to build your first Dapp on Polygon and use Polygon APIs?
Check out the following tutorials to learn how to build on Polygon:
- How to Code and Deploy a Polygon Smart Contract
- Polygon SDK Examples
- Polygon API FAQ
- How to Add Polygon to Metamask
For full documentation on the available endpoints for alchemy-sdk, check the github repo: