Polygon - Getting Started (REST API)

This article covers the steps required to create a Polygon wallet and generate addresses and private keys. It includes an example of checking the address balance and transferring funds to another Polygon address.

πŸ“˜

The following steps and examples are based on Polygon Amoy (Testnet).

Steps

Step_1: Generate a Mnemonic

Generating a mnemonic means creating a 24-word phrase that will be the foundation for your wallet. This phrase is like a master key from which all your wallet addresses and their private keys can be generated.

  • Tatum does not store Mnemonics and or Private Keys.
  • The responsibility of keeping your Mnemonics and Private Keys secure rests solely with you, the User.
  • Additional information about Mnemonics and Private Keys is available in the following article.

Example request:

  • The response returns a Mnemonic and a Polygon XPUB
curl --location 'https://api.tatum.io/v3/polygon/wallet' \
--header 'x-api-key: {YOUR_API_KEY}'
//Response:
{
    "xpub": "xpub6EXnqPguNo9tXDzoJgYD4HDLd9MCPWXZK6hKhx4PDTUbTiEkMS3q3hPBDWkHcFV9jLrzCVeGDXcXumvZ4sG5rZwWebS5sN12GmPj7f82DiZ",
    "mnemonic": "wasp mom comfort human hero nest gain february cage enemy pause nation brick trumpet #### #### ####"
}

πŸ“˜

Be mindful of Tatum's Derivation Path.

Step_2: Generate a Polygon Private Key from Mnemonic and index

Creating a private key is creating a unique and secret key that belongs to you. This key is crucial for accessing and controlling your funds.

Example request:

  • The response returns a Polygon Private Key based on index 1.
curl --location 'https://api.tatum.io/v3/polygon/wallet/priv' \
--header 'Content-Type: application/json' \
--header 'x-api-key: {YOUR_API_KEY}' \
--data '{
    "mnemonic": "wasp mom comfort human hero nest gain february cage enemy pause nation brick trumpet  #### #### ####",
    "index": 1
}'
//Response:
{
    "key": "####"
}

Step_3: Generate a Polygon address from XPUB and index

Generating an address is about creating a public address where others can send you funds. It's like an account number in the blockchain world.

Example request:

  • The response returns a Polygon Externally Owned Address (EOA) based on index 1
curl --location 'https://api.tatum.io/v3/polygon/address/xpub6E633e5b4gYG2NnKxf99VcjRBpLv2Szcuoypn1wBkAbgMmubtNW9EsqzoAQZHdNX23HG4Lf7bNZUAVcBsZcuka2GRY1HQtgHNa2Pyfw2iGW/1' \
--header 'x-api-key: {YOUR_API_KEY}'
//Response:
{
    "address": "0x34eec89a57984280921f19d13c9e84f875b5ee78"
}

Step_4: Add funds to the Polygon address

  • We take the address generated in step_3.
  • For testnet, you can get free coins from a faucet.
{
    "address": "0x34eec89a57984280921f19d13c9e84f875b5ee78"
}

Step_5: Check the balance of a Polygon address

Request example:

  • The response returns the balance of a Polygon address in Matic
curl --location 'https://api.tatum.io/v3/polygon/account/balance/0x34eec89a57984280921f19d13c9e84f875b5ee78' \
--header 'x-api-key: {YOUR_API_KEY}'
//Response:
"balance": "0.005"

Step_6: Estimate the fees of a Polygon transaction

Tatum Fee estimates endpoints return values in Wei. Find more about EVM Fee Estimate in the following article.

Request example:

  • The response returns an estimation of the potentially required Gas Used via "GasLimit" and the "GasPrice". Additional information is available in the following article.
curl --location 'https://api.tatum.io/v3/polygon/gas' \
--header 'Content-Type: application/json' \
--header 'x-api-key: {YOUR_API_KEY}' \
--data '{
    "from": "0x34eec89a57984280921f19d13c9e84f875b5ee78",
    "to": "0xf52b4aad2fc99e1b30de50afca41d6d4d3d870c7",
    "amount": "0.004"
}'
//Response:
{
    "gasLimit": 21000,
    "gasPrice": "31000000000"
}

Step_7: Check the current "nonce" value from the sender's address

The "nonce" is a unique and incremental counter used to ensure that each transaction is processed only once, preventing replay attacks. EVM transactions require this. Find more about the "nonce" in the following article.

Request example:

  • The response returns the current "nonce" value of a Polygon address
curl --location 'https://api.tatum.io/v3/polygon/transaction/count/0x34eec89a57984280921f19d13c9e84f875b5ee78' \
--header 'x-api-key: {YOUR_API_KEY}'
//Response:
0

Step_8: Sign and broadcast a Polygon transaction

Request example:

  • The response returns a transaction hash
curl --location 'https://api.tatum.io/v3/polygon/transaction' \
--header 'Content-Type: application/json' \
--header 'x-api-key: {YOUR_API_KEY}' \
--data '{
    "to": "0xf52b4aad2fc99e1b30de50afca41d6d4d3d870c7",
    "currency": "MATIC",
    "amount": "0.04",
    "fee": {
        "gasPrice": "31", // In GWEI!
        "gasLimit": "21000"
    },
    "nonce": 0,
    "fromPrivateKey": "SENDER_ADDRESS_PRIVATE_KEY"
}'
//Response:
{
    "txId": "0xb17382da5ae9d4434f822f1915c2b90f3d4cd37d7259f64ab7cac14ade38bf1b"
}

Step_9: Get the details of a Polygon transaction

Request example:

  • The response returns the details of a Polygon transaction hash
curl --location 'https://api.tatum.io/v3/polygon/transaction/0xb17382da5ae9d4434f822f1915c2b90f3d4cd37d7259f64ab7cac14ade38bf1b' \
--header 'x-api-key: {YOUR_API_KEY}'
//Response:
{
    "transactionHash": "0xb17382da5ae9d4434f822f1915c2b90f3d4cd37d7259f64ab7cac14ade38bf1b",
    "blockHash": "0x9e367349de48dfe2e73a9b8d5fad19593509e6ee63c683474b7be6dca67a8920",
    "blockNumber": 8626218,
    "from": "0x34eec89a57984280921f19d13c9e84f875b5ee78",
    "gas": 21000,
    "gasPrice": 31000000000,
    "input": "0x",
    "nonce": 0,
    "to": "0xf52b4aad2fc99e1b30de50afca41d6d4d3d870c7",
    "transactionIndex": 0,
    "value": "4000000000000000",
    "type": "0x0",
    "chainId": "0x13882",
    "contractAddress": null,
    "cumulativeGasUsed": "21000",
    "effectiveGasPrice": "0x737be7600",
    "gasUsed": "21000",
    "logs": [
        {
            "address": "0x0000000000000000000000000000000000001010",
            "topics": [
                "0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4",
                "0x0000000000000000000000000000000000000000000000000000000000001010",
                "0x00000000000000000000000034eec89a57984280921f19d13c9e84f875b5ee78",
                "0x000000000000000000000000f52b4aad2fc99e1b30de50afca41d6d4d3d870c7"
            ],
            "data": "0x000000000000000000000000000000000000000000000000000e35fa931a0000000000000000000000000000000000000000000000000000000f73647820d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013d69e506d000000000000000000000000000000000000000000000000000000e35fa931a0000",
            "blockNumber": 8626218,
            "transactionHash": "0xb17382da5ae9d4434f822f1915c2b90f3d4cd37d7259f64ab7cac14ade38bf1b",
            "transactionIndex": 0,
            "blockHash": "0x9e367349de48dfe2e73a9b8d5fad19593509e6ee63c683474b7be6dca67a8920",
            "logIndex": 0,
            "removed": false
        },
        {
            "address": "0x0000000000000000000000000000000000001010",
            "topics": [
                "0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b422b415d95645b9adcfd678cb1d63",
                "0x0000000000000000000000000000000000000000000000000000000000001010",
                "0x00000000000000000000000034eec89a57984280921f19d13c9e84f875b5ee78",
                "0x0000000000000000000000004ca9ff871c7aa1e7b64e1eae110835f68d6a0bd4"
            ],
            "data": "0x00000000000000000000000000000000000000000000000000025014bfbae1880000000000000000000000000000000000000000000000000011c37937e080000000000000000000000000000000000000000000000002430d3e8d241885625a000000000000000000000000000000000000000000000000000f736478259e780000000000000000000000000000000000000000000002430d40dd38d84043e2",
            "blockNumber": 8626218,
            "transactionHash": "0xb17382da5ae9d4434f822f1915c2b90f3d4cd37d7259f64ab7cac14ade38bf1b",
            "transactionIndex": 0,
            "blockHash": "0x9e367349de48dfe2e73a9b8d5fad19593509e6ee63c683474b7be6dca67a8920",
            "logIndex": 1,
            "removed": false
        }
    ],
    "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000120008000000000000000000000000000000000000000000000000000000000800000000000010000000100000000000000000000000200000000000000000000000000000000000080000000000000000000000000000000000000000000000000008000000000000000000000000000200000000040060000000000000000000008000000000000000000000000004000000000000000000001000000000000000000000000800000108000000000000000000000000000000000000000000000000000000000000000000000100000",
    "status": true,
    "hash": "0xb17382da5ae9d4434f822f1915c2b90f3d4cd37d7259f64ab7cac14ade38bf1b"
}

Good to Know

  • Fee Estimates may fluctuate one way or the other, within seconds.
    • Tatum Mainnet Fee Estimates are generally in line with other providers.
    • The Testnet Fee Estimate is generally unreliable. Since testnet coins have "no value", users tend to set random amounts greatly skewing the estimates.
  • Be mindful of Tatum's derivation path. Find additional information in the following article.
  • To transfer tokens on the POLYGON network, check the following article.
  • Familiarize yourself with the Safety & Security Basics in the following article.