Automating with Node.js: Using Axios to Call REST APIs and Fetch Data

![[Node.JS 자동화] Node.js 자동화 프로그래밍, 반복 작업을 AI와 함께 자동화하는 실전 개발(프롤로그)](/uploads/editor/Node-JS-%EC%9E%90%EB%8F%99%ED%99%94-Node-js-%EC%9E%90%EB%8F%99%ED%99%94-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-%EB%B0%98%EB%B3%B5-%EC%9E%91%EC%97%85%EC%9D%84-AI%EC%99%80-%ED%95%A8%EA%BB%98-%EC%9E%90%EB%8F%99%ED%99%94%ED%95%98%EB%8A%94-%EC%8B%A4%EC%A0%84-%EA%B0%9C%EB%B0%9C-%ED%94%84%EB%A1%A4%EB%A1%9C%EA%B7%B8-20260702-e78eab.webp)
What is an API?
When developing programs, you often need to retrieve data from or send information to other services.
For example:
Currency exchange rates
Weather information
Stock prices
News listings
ChatGPT
Map services
Sending text messages
These services typically provide an API (Application Programming Interface).
An API is essentially a communication channel between programs.
Instead of users opening a website and clicking buttons, programs directly request data and receive results.
Mastering the ability to interact with APIs is key to building effective automation programs.
What is a REST API?
The most commonly used API style today is REST API.
A REST API uses HTTP to exchange data, and most web services rely on it.
The main request methods include:
Method | Description |
|---|---|
GET | Retrieve data |
POST | Create or send data |
PUT | Edit data |
DELETE | Delete data |
This tutorial will focus on the most commonly used methods: GET and POST.
What is Axios?
There are several ways to call APIs in Node.js,
but Axios is currently the most widely used library.
Axios offers:
Simplified usage
Ease of handling JSON data
Easy error handling
It's one of the most commonly used HTTP libraries in real-world applications
You'll continue to use Axios when integrating with external services like OpenAI API.
Installing Axios
First, install Axios in your project.
Run the following command in your terminal:
npm install axios
Once installed, it will automatically be added to package.json.
Loading Axios
After installation, load Axios in your program.
const axios = require("axios");
This single line prepares you to use Axios.
What is a GET Request?
A GET request retrieves data from a server.
Examples include:
Fetching news listings
Currency exchange rate lookup
Weather information retrieval
Stock price inquiry
Most retrieval functions use GET requests.
Here's a simple example of a GET request:
const axios = require("axios");
async function test() {
const response = await axios.get("https://jsonplaceholder.typicode.com/posts/1");
console.log(response.data);
}
test();
Running this program outputs the JSON data received from the server.
Understanding the Response Object
Axios results are stored in a response object.
The most commonly used properties include:
response.data
response.status
response.headers
The most frequently used is
response.data
as it contains the actual data sent by the server.
What is JSON?
APIs typically deliver data in JSON (JavaScript Object Notation) format.
For example:
{
"name": "Hong Gildong",
"age": 30
}
In Node.js, you can work with JSON data as if it were a regular object.
console.log(data.name);
console.log(data.age);
This allows easy access to specific values.
What is a POST Request?
A POST request sends data to the server.
Common uses include:
Login
User registration
Creating posts
AI queries
Data storage
For example:
await axios.post(url, {
title: "Hello",
content: "Node.js tutorial."
});
You can send an object along with the request.
Request Headers
Some APIs require additional information to be sent alongside requests.
This often includes:
API Key
Authentication tokens
Content-Type
For example:
await axios.get(url, {
headers: {
Authorization: "Bearer API_KEY"
}
});
This is how OpenAI API handles authentication.
Using Query Strings
When passing search criteria or page numbers, use query strings.
For example:
https://example.com/news?page=1
The ?page=1 part is the query string.
In Axios, you can use the params option:
await axios.get(url, {
params: {
page: 1,
size: 20
}
});
Axios automatically constructs the URL.
Error Handling
APIs don't always work perfectly.
Network errors or server downtime can occur,
so proper error handling is essential.
try {
const response = await axios.get(url);
console.log(response.data);
} catch (error) {
console.log(error.message);
}
In real-world applications, it's standard practice to wrap all API calls in try...catch blocks.
Understanding Async/Await
Axios operates asynchronously,
so most code uses:
async
await
These keywords allow the program to wait for a response before proceeding, making code easier to read and maintain.
You'll continue using async/await in all future tutorials.
Where Will Axios Be Used in Automation?
Axios appears in nearly every project covered in this tutorial series.
For example:
Calling OpenAI API
News collection
Keyword searches
Currency exchange rate lookup
Stock price monitoring
Image downloads
Telegram API integration
Discord Webhook usage
Custom server APIs
All of these use Axios to exchange data.
Axios is arguably the most commonly used library in automation programs.
Why Mastering Axios Is Important
Most Node.js automation programs connect with external services.
You'll need to call APIs even when using AI,
retrieve news via APIs,
and send Telegram messages through API calls.
In essence, the core of an automation program lies in how freely you can retrieve and send data.
Mastering Axios allows you to easily integrate with most external services.
Conclusion
This tutorial covered using Axios to call REST APIs, including GET and POST requests, handling JSON data, managing headers, query strings, and error handling—key features used in automation programs.
You'll apply these Axios skills when integrating with external services like OpenAI API, Telegram API, and news APIs in future tutorials.
In the next lesson, we'll explore how to connect MySQL with Node.js for efficient data storage and management—a crucial step for organizing data collected by your automation programs.
FAQ
What is an API?
An API (Application Programming Interface) is a communication channel that allows programs to interact with each other, enabling data exchange between services without user intervention.
Why is Axios popular for Node.js APIs?
Axios is widely used in Node.js because it simplifies API calls, handles JSON data efficiently, offers easy error handling, and is commonly adopted in real-world applications.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Node.JS 자동화] 4강. Axios로 REST API 호출하고 데이터 가져오기