# Polygon ScanのAPI調査

By [Y's note](https://paragraph.com/@y-s-note) · 2022-08-16

---

### はじめに

[@yutakikuchi\_](https://twitter.com/yutakikuchi_) です。 BlockChainのTransaction履歴を見れる xxscanは見たことある方多いと思いますが、履歴を見るだけではなく、Transactionのデータを自動で引っ張ってきて処理を行うなど後続の処理を自動化したい場合はAPIを利用する良いと思います。

ETHとPolygonのAPIは下記がDocumentとなっています。ざっとAPI Documentを見た感じ、EndpointのBaseとなるURLは異なるものの、EndpointのPath, Interfaceともに同じ構成になっているようです。

[https://docs.etherscan.io/api-endpoints/accounts](https://docs.etherscan.io/api-endpoints/accounts)

[https://docs.polygonscan.com/api-endpoints/accounts](https://docs.polygonscan.com/api-endpoints/accounts)

### Polygon ScanのAPIで特定ContractのEvent listを取得する

まずはじめに、Polygon ScanのAPIを利用するには、下記の作業を行う必要があります。

*   Polygon scan上にアカウント登録。[こちらから](https://polygonscan.com/register)
    
*   API Keyの発行。[こちらから](https://polygonscan.com/myapikey)
    

APIの無償プランの場合、秒間 5 calls、1日に10万 callsという制限があります。**またPro planに入らないとcallできないAPIのendpointがいくつか存在します。**

![API Plans](https://storage.googleapis.com/papyrus_images/91d4bb317a02f1101f9cef959bc32a85c28f15efb8d478ff480f6dac86adb6a6.png)

API Plans

上の準備が完了すると、APIをcallできます。**以下では本番のPolygonを使わずにtestnets(Mumbai)のAPIをcallする内容で記載します。**

特定のcontract addressについて、例えばERC-721のトランザクション event一覧を取得した場合は、[こちに掲載されたURL](https://docs.polygonscan.com/api-endpoints/accounts#get-a-list-of-erc-20-token-transfer-events-by-address)に対してAPIのリクエストを行います。下記は[こちら](https://mumbai.polygonscan.com/address/0xd6f9e7defa52ac67cdfd6a9a352ce6e0a9684b0a#events)のPolygom mumbai scanに掲載されているトランザクションのサンプルのRequest URLになります。

> [https://api-testnet.polygonscan.com/api](https://api-testnet.polygonscan.com/api)?module=account &action=tokennfttx&contractaddress=0xd6f9e7defa52ac67cdfd6a9a352ce6e0a9684b0a66&page=1&offset=5&sort=asc&apikey=YourApiKeyToken

Response Bodyについては一部のみ掲載しますが、下記のようなJSON形式の各種Event情報が配列形式で取得できます。また**上記のaccountに関する情報を取得するendpoint以外にも、transaction一覧、log一覧、block一覧を取得するAPIもあるので、興味がある方は色々と試してみると良い**と思います。

    {
    "status": "1",
    "message": "OK",
    "result": [
    {
    "blockNumber": "24956800",
    "timeStamp": "1644919485",
    "hash": "0x42a2e8f4379b09bf74eefab614854fc6a7506ed22bcb95c113c956511291cb5d",
    "nonce": "0",
    "blockHash": "0xfa59f4483ca2f1fa4536e4f51f529ddf3b67680772f34be55c1bfe1844f9a103",
    "from": "0x9ea7783b3e45213e0da1e9fe7bca3b7362f052b1",
    "contractAddress": "0xd6f9e7defa52ac67cdfd6a9a352ce6e0a9684b0a",
    "to": "0xeede4b338f35e38b8c0a79193e766eb5fc937abd",
    "tokenID": "592",
    "tokenName": "MoonieNFT",
    "tokenSymbol": "MOONIE",
    "tokenDecimal": "0",
    "transactionIndex": "19",
    "gas": "0",
    "gasPrice": "0",
    "gasUsed": "0",
    "cumulativeGasUsed": "0",
    "input": "deprecated",
    "confirmations": "2686826"
    }, 
    

### DuneなどのAnalyticsを利用する方法もある

上では公開されたWeb APIを利用してPolygon scanで出力されるデータを取得しましたが、Dune Analyticsなど、SQLベースでデータを抽出する便利なツールもあるので、そちらは今後のentryにしたいと思います。

[https://dune.com/browse/dashboards](https://dune.com/browse/dashboards)

---

*Originally published on [Y's note](https://paragraph.com/@y-s-note/polygon-scan-api)*
