Skip to content

Latest commit

 

History

History
180 lines (141 loc) · 4.5 KB

quickstart.md

File metadata and controls

180 lines (141 loc) · 4.5 KB
description sidebar_position
Get started with the Gas APIs.
1

import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem";

Quickstart

This quickstart shows you how to call the Gas API using JavaScript. You can also use a tool such as curl or Postman to call the REST APIs.

:::tip View the API reference content to view the curl command for each API. :::

Prerequisites

Initialize a new project directory

mkdir new_project
cd new_project
npm init -y

Install required packages

Install the axios package:

npm install axios

Install the dotenv package:

npm install dotenv

Create your .env file

Create a .env file at the project root and add the following data:

:::caution warning Do not commit the .env file to your repository if it contains sensitive data. You can create a .gitignore file to prevent accidentally committing the file. :::

INFURA_API_KEY=<YOUR-API-KEY>
INFURA_API_KEY_SECRET=<YOUR-API-KEY-SECRET>

Replace the Infura project credential placeholders with your own.

:::note The INFURA_API_KEY_SECRET is optional and only necessary if you are using an API key secret to authenticate requests. :::

Create your script

The Gas API supports multiple request formats, and you can call the methods with or without specifying an API key secret.

Create a file (in this example index.js):

touch index.js

Copy the following code into your script:

:::info note If using a network other than Ethereum Mainnet, update the chainId value (1) in the code to an alternate supported network. :::

const axios = require("axios");
require("dotenv").config();

// The chain ID of the supported network
const chainId = 1;

(async () => {
  try {
    const { data } = await axios.get(
      `https://gas.api.infura.io/v3/${process.env.INFURA_API_KEY}/networks/${chainId}/suggestedGasFees`
    );
    console.log("Suggested gas fees:", data);
  } catch (error) {
    console.log("Server responded with:", error);
  }
})();
const axios = require("axios");
require("dotenv").config();

const Auth = Buffer.from(
  process.env.INFURA_API_KEY + ":" + process.env.INFURA_API_KEY_SECRET
).toString("base64");

// The chain ID of the supported network
const chainId = 1;

(async () => {
  try {
    const { data } = await axios.get(
      `https://gas.api.infura.io/networks/${chainId}/suggestedGasFees`,
      {
        headers: {
          Authorization: `Basic ${Auth}`,
        },
      }
    );
    console.log("Suggested gas fees:", data);
  } catch (error) {
    console.log("Server responded with:", error);
  }
})();

Run the script

node index.js

The result should look similar to:

Suggested gas fees: {
  low: {
    suggestedMaxPriorityFeePerGas: "0.05", // The gas price in gwei
    suggestedMaxFeePerGas: "24.086058416", // The gas price in gwei
    minWaitTimeEstimate: 15000,
    maxWaitTimeEstimate: 30000
  },
  medium: {
    suggestedMaxPriorityFeePerGas: "0.1", // The gas price in gwei
    suggestedMaxFeePerGas: "32.548678862", // The gas price in gwei
    minWaitTimeEstimate: 15000,
    maxWaitTimeEstimate: 45000
  },
  high: {
    suggestedMaxPriorityFeePerGas: "0.3", // The gas price in gwei
    suggestedMaxFeePerGas: "41.161299308", // The gas price in gwei
    minWaitTimeEstimate: 15000,
    maxWaitTimeEstimate: 60000
  },
  estimatedBaseFee: "24.036058416",
  networkCongestion: 0.7143,
  latestPriorityFeeRange: [ "0.1", "20" ],
  historicalPriorityFeeRange: [ "0.007150439", "113" ],
  historicalBaseFeeRange: [ "19.531410688", "36.299069766" ],
  priorityFeeTrend: "down",
  baseFeeTrend: "down"
}