|
1 | 1 | --- |
2 | 2 | title: Polygon PoS API Quickstart |
3 | | -description: How to get started building on Polygon PoS and using the JSON-RPC API |
| 3 | +description: How to get started building on Polygon PoS and using |
4 | 4 | subtitle: How to get started building on Polygon PoS and using the JSON-RPC API |
5 | | -slug: reference/polygon-pos-api-quickstart |
| 5 | +slug: docs/polygon-pos |
6 | 6 | --- |
7 | 7 |
|
8 | | -<Tip title="Don’t have an API key?" icon="star"> |
9 | | - Sign up to start building on Polygon. [Get started for free](https://dashboard.alchemy.com/signup) |
| 8 | +<Tip title="Don't have an API key?" icon="star"> |
| 9 | + Build faster with production-ready APIs, smart wallets and rollup infrastructure across 70+ chains. Create your free Alchemy API key and{" "} |
| 10 | + <a href="https://dashboard.alchemy.com/signup">get started today</a>. |
10 | 11 | </Tip> |
11 | 12 |
|
12 | | -# Getting Started Instructions |
| 13 | +## Send Your First Request on Alchemy |
13 | 14 |
|
14 | | -## 1. Choose a package manager (npm or yarn) |
15 | | - |
16 | | -For this guide, we will be using npm or yarn as our package manager to install Viem or Ethers.js. |
17 | | - |
18 | | -### npm |
19 | | - |
20 | | -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](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) |
21 | | - |
22 | | -### yarn |
23 | | - |
24 | | -To get started with `yarn`, follow these steps: [https://classic.yarnpkg.com/lang/en/docs/install](https://classic.yarnpkg.com/lang/en/docs/install/#mac-stable) |
25 | | - |
26 | | -## 2. Set up your project (npm or yarn) |
| 15 | +Let's use the [`viem`](https://www.npmjs.com/package/viem) package to create an Ethereum client connected to Alchemy and fetch the latest block number! |
27 | 16 |
|
28 | 17 | <CodeGroup> |
29 | | - ```text Shell (npm) |
30 | | - mkdir alchemy-polygon-api |
31 | | - cd alchemy-polygon-api |
32 | | - npm init --yes |
| 18 | + ```text npm |
| 19 | + npm install --save viem |
33 | 20 | ``` |
34 | 21 |
|
35 | | - ```text Shell (yarn) |
36 | | - mkdir alchemy-polygon-api |
37 | | - cd alchemy-polygon-api |
38 | | - yarn init --yes |
| 22 | + ```text yarn |
| 23 | + yarn add viem |
39 | 24 | ``` |
40 | 25 | </CodeGroup> |
41 | 26 |
|
42 | | -## 3. Install Web3 Library |
43 | | - |
44 | | -Run the following command to install Viem or Ethers.js with npm or yarn. |
| 27 | +## Create Client Connected to Alchemy |
45 | 28 |
|
46 | 29 | <CodeGroup> |
47 | | - ```text Viem (npm) |
48 | | - npm install viem |
49 | | - ``` |
50 | | - |
51 | | - ```text Viem (yarn) |
52 | | - yarn add viem |
53 | | - ``` |
54 | | - |
55 | | - ```text Ethers.js (npm) |
56 | | - npm install ethers |
57 | | - ``` |
58 | | - |
59 | | - ```text Ethers.js (yarn) |
60 | | - yarn add ethers |
61 | | - ``` |
| 30 | +```js |
| 31 | +import { createPublicClient, http } from "viem"; |
| 32 | +import { mainnet } from "viem/chains"; |
| 33 | + |
| 34 | +const client = createPublicClient({ |
| 35 | + chain: mainnet, |
| 36 | + transport: http("https://polygon-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_API_KEY"), |
| 37 | +}); |
| 38 | +``` |
62 | 39 | </CodeGroup> |
63 | 40 |
|
64 | | -## 4. Make your first request |
| 41 | +Now that you've created a client connected to Alchemy, you can continue with some basics: |
65 | 42 |
|
66 | | -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 one of the following code snippets into the file. |
| 43 | +## Get Latest Block Number |
67 | 44 |
|
68 | 45 | <CodeGroup> |
69 | | - ```javascript Viem |
70 | | - import { createPublicClient, http } from 'viem' |
71 | | - import { polygon } from 'viem/chains' |
72 | | - |
73 | | - const client = createPublicClient({ |
74 | | - chain: polygon, |
75 | | - transport: http('https://polygon-mainnet.g.alchemy.com/v2/demo') // Replace 'demo' with your API Key |
76 | | - }) |
| 46 | +```js |
| 47 | +const blockNumber = await client.getBlockNumber(); |
| 48 | +console.log("Current block number:", blockNumber); |
| 49 | +``` |
| 50 | +</CodeGroup> |
77 | 51 |
|
78 | | - async function main() { |
79 | | - const latestBlock = await client.getBlockNumber() |
80 | | - console.log('The latest block number is', latestBlock) |
81 | | - } |
| 52 | +## Get an Address Balance |
82 | 53 |
|
83 | | - main() |
84 | | - ``` |
| 54 | +<CodeGroup> |
| 55 | +```js |
| 56 | +const balance = await client.getBalance({ address: "0xab5801a7d398351b8be11c439e05c5b3259aec9b" }); |
| 57 | +console.log("Balance (ETH):", Number(balance) / 1e18); |
| 58 | +``` |
| 59 | +</CodeGroup> |
85 | 60 |
|
86 | | - ```javascript Ethers.js |
87 | | - import { JsonRpcProvider } from 'ethers' |
| 61 | +## Read Block Data |
88 | 62 |
|
89 | | - const provider = new JsonRpcProvider('https://polygon-mainnet.g.alchemy.com/v2/demo') // Replace 'demo' with your API Key |
| 63 | +<CodeGroup> |
| 64 | +```js |
| 65 | +const block = await client.getBlock({ |
| 66 | + blockNumber: blockNumber, // from previous example |
| 67 | +}); |
| 68 | +console.log(block); |
| 69 | +``` |
| 70 | +</CodeGroup> |
90 | 71 |
|
91 | | - async function main() { |
92 | | - const latestBlock = await provider.getBlockNumber() |
93 | | - console.log('The latest block number is', latestBlock) |
94 | | - } |
| 72 | +## Fetch a Transaction by Hash |
95 | 73 |
|
96 | | - main() |
97 | | - ``` |
| 74 | +<CodeGroup> |
| 75 | +```js |
| 76 | +const tx = await client.getTransaction({ hash: "0xYOUR_TX_HASH" }); |
| 77 | +console.log(tx); |
| 78 | +``` |
98 | 79 | </CodeGroup> |
99 | 80 |
|
100 | | -## 5. Run script |
101 | | - |
102 | | -To run the above node script, use cmd `node index.js`, and you should see the output. |
| 81 | +## Fetch Transaction Receipt |
103 | 82 |
|
104 | 83 | <CodeGroup> |
105 | | - ```text shell |
106 | | - The latest block number is 62250231n |
107 | | - ``` |
| 84 | +```js |
| 85 | +const receipt = await client.getTransactionReceipt({ |
| 86 | + hash: "0xYOUR_TX_HASH" |
| 87 | +}); |
| 88 | +console.log(receipt); |
| 89 | +``` |
108 | 90 | </CodeGroup> |
109 | 91 |
|
110 | | -*** |
111 | | - |
112 | 92 | # Polygon Tutorials |
113 | 93 |
|
114 | | -You must not stop here! Want to build your first Dapp on Polygon and use Polygon APIs? |
| 94 | +Want to build your first Dapp on Polygon and use Polygon APIs? |
115 | 95 |
|
116 | 96 | Check out the following tutorials to learn how to build on Polygon: |
117 | 97 |
|
|
0 commit comments