API

Exchange Rate API를 활용해 환율 계산기 구현하기

주연배 2025. 5. 15. 23:33

 

1. API KEY 발급

https://www.exchangerate-api.com/

 

ExchangeRate-API - Free & Pro Currency Converter API

Get the data you need from our exchange rate API - it's perfect for spreadsheets, apps, SaaS, E-Commerce and more!

www.exchangerate-api.com

 

 

여기서 API Key를 발급 받을 수 있다.

발급 받은 API는 프로젝트 루트에 .env 파일을 만들어 암호화 한다.

 

vs code로 가서 서버 폴더 루트 위치에 .env 파일 만들기

EXCHANGE_API_KEY=82f6271b07b1caa96d70f816

 

2. Node.js로 환율 API 서버 만들기

exchangeRateRoutes.js

require('dotenv').config();
const express = require('express')
const router = express.Router()
const axios = require('axios')

router.get('/', async (req, res) => {
    try {
        // response -> 전체 응답 객체
        const response = await axios.get(`https://v6.exchangerate-api.com/v6/${process.env.EXCHANGE_API_KEY}/latest/KRW`);

        //호주 환율만 가져오기
        const audRate = response.data.conversion_rates.AUD
        res.json({ rate: audRate })
    } catch (err) {
        res.status(500).json({ message: '환율 데이터 불러올 수 없음' })
    }
})
module.exports = router;

 

⭐ 공식 문서

 

ExchangeRate-API - Standard Request Type Documentation

Standard Requests Documentation for the Standard ExchangeRate-API endpoint.

www.exchangerate-api.com

 

 

  • response전체 응답 객체를 가지고 있다. 
  • exchangerate-api에서 제공하는 전체 응답 객체를 get으로 가져온다.
  • audRate는 전체 응답 객체 중에서 호주 환율만 가져온다.

const audRate = response.data.conversion_rates.AUD

 

여기서 호주 환율은 AUD이므로 위와 같이 가져올 수 있다.

 

3. API 테스트 (with postman)

api가 잘 작동하는지 확인하기 위해 postman으로 테스트 해 봤다!

method : get
url : https://v6.exchangerate-api.com/v6/발급받은 API Key/latest/KRW

 

 

send를 눌러서 확인해 보면 잘 나오는 것을 확인할 수 있다!!