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
- A valid Web3 API key and API key secret.
- Node.js and npm installed.
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:
INFURA_API_KEY=<API-KEY>
INFURA_API_KEY_SECRET=<API-KEY-SECRET>
Replace the Infura project credential placeholders with your own.
danger
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.
Create your script
touch index.js
Copy the following code into your script:
info
If using a network other than Ethereum Mainnet, then update the chainId
value (1
) in the code to an alternate
supported network.
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',
suggestedMaxFeePerGas: '24.086058416',
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 30000
},
medium: {
suggestedMaxPriorityFeePerGas: '0.1',
suggestedMaxFeePerGas: '32.548678862',
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 45000
},
high: {
suggestedMaxPriorityFeePerGas: '0.3',
suggestedMaxFeePerGas: '41.161299308',
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'
}