환율 정보 (API 구현)

••266 read
미국 달러USD
1342.4
살 때 (TTS)1355.82
팔 때 (TTB)1328.97
기준일: 2026. 10. 19
일본 옌JPY(100)
850.24
살 때 (TTS)858.74
팔 때 (TTB)841.73
기준일: 2026. 10. 19
유로EUR
1505.17
살 때 (TTS)1520.22
팔 때 (TTB)1490.11
기준일: 2026. 10. 19
위안화CNH
201.03
살 때 (TTS)203.04
팔 때 (TTB)199.01
기준일: 2026. 10. 19
홍콩 달러HKD
171.05
살 때 (TTS)172.76
팔 때 (TTB)169.33
기준일: 2026. 10. 19
호주 달러AUD
934.78
살 때 (TTS)944.12
팔 때 (TTB)925.43
기준일: 2026. 10. 19
캐나다 달러CAD
950.14
살 때 (TTS)959.64
팔 때 (TTB)940.63
기준일: 2026. 10. 19
영국 파운드GBP
1788.61
살 때 (TTS)1806.49
팔 때 (TTB)1770.72
기준일: 2026. 10. 19
스위스 프랑CHF
1628.24
살 때 (TTS)1644.52
팔 때 (TTB)1611.95
기준일: 2026. 10. 19
싱가포르 달러SGD
1048.83
살 때 (TTS)1059.31
팔 때 (TTB)1038.34
기준일: 2026. 10. 19
뉴질랜드 달러NZD
758.52
살 때 (TTS)766.1
팔 때 (TTB)750.93
기준일: 2026. 10. 19
아랍에미리트 디르함AED
365.51
살 때 (TTS)369.16
팔 때 (TTB)361.85
기준일: 2026. 10. 19
바레인 디나르BHD
3555.56
살 때 (TTS)3591.11
팔 때 (TTB)3520
기준일: 2026. 10. 19
브루나이 달러BND
1048.83
살 때 (TTS)1059.31
팔 때 (TTB)1038.34
기준일: 2026. 10. 19
덴마아크 크로네DKK
201.36
살 때 (TTS)203.37
팔 때 (TTB)199.34
기준일: 2026. 10. 19
인도네시아 루피아IDR(100)
7.46
살 때 (TTS)7.53
팔 때 (TTB)7.38
기준일: 2026. 10. 19
쿠웨이트 디나르KWD
4358.44
살 때 (TTS)4402.02
팔 때 (TTB)4314.85
기준일: 2026. 10. 19
말레이지아 링기트MYR
328.9
살 때 (TTS)332.18
팔 때 (TTB)325.61
기준일: 2026. 10. 19
노르웨이 크로네NOK
142.18
살 때 (TTS)143.6
팔 때 (TTB)140.75
기준일: 2026. 10. 19
사우디 리얄SAR
357.72
살 때 (TTS)361.29
팔 때 (TTB)354.14
기준일: 2026. 10. 19
스웨덴 크로나SEK
135.24
살 때 (TTS)136.59
팔 때 (TTB)133.88
기준일: 2026. 10. 19
태국 바트THB
40.16
살 때 (TTS)40.56
팔 때 (TTB)39.75
기준일: 2026. 10. 19
로직 설명
  1. API URL - https://oapi.koreaexim.go.kr/site/program/financial/exchangeJSON
  2. API KEY - authkey ( 참고 : https://www.koreaexim.go.kr/ir/HPHKIR019M01#tab1 )
  3. 파라미터 : authKey, searchdate, data
  4. 사용한 로직
    /**
     * 환율 관련 비즈니스 로직을 처리하는 서비스
     */
    export class ExchangeRateService {
    	/**
    	 * @param {string} authkey - 한국수출입은행 API 키
    	 */
    	constructor(authkey) {
    		this.authkey = authkey;
    	}
    
    	/**
    	 * 지정된 날짜의 환율 데이터를 가져옵니다. (캐시 우선 전략)
    	 * @param {string} date - 조회 날짜 (YYYYMMDD)
    	 */
    	async getRatesByDate(date) {
    		try {
    
    			const apiData = await this._fetchExchangeApi(date);
    
    			if (!apiData || apiData.length === 0) {
    				return [];
    			}
    
    			// 3. API 데이터를 DB 형식에 맞게 정제하여 저장
    			const formattedData = this._formatApiData(apiData, date);
    			return formattedData;
    		} catch (error) {
    			console.error('[ExchangeRateService Error]:', error);
    			throw error;
    		}
    	}
    
    	/** 내부 API 호출 전용 (private-like) */
    	async _fetchExchangeApi(date) {
    		const url = `https://oapi.koreaexim.go.kr/site/program/financial/exchangeJSON?authkey=${this.authkey}&searchdate=${date}&data=AP01`;
    		const res = await fetch(url);
    		if (!res.ok) throw new Error('API 응답 실패');
    		return await res.json();
    	}
    
    	/** 데이터를 DB 스키마에 맞게 매핑 */
    	_formatApiData(apiData, date) {
    		return apiData.map((item) => ({
    			cur_unit: item.cur_unit,
    			cur_nm: item.cur_nm,
    			ttb: item.ttb?.replace(/,/g, ''),
    			tts: item.tts?.replace(/,/g, ''),
    			deal_bas_r: item.deal_bas_r?.replace(/,/g, ''),
    			bkpr: item.bkpr?.replace(/,/g, ''),
    			yy_efee_r: item.yy_efee_r?.replace(/,/g, ''),
    			ten_dd_efee_r: item.ten_dd_efee_r?.replace(/,/g, ''),
    			kftc_deal_bas_r: item.kftc_deal_bas_r?.replace(/,/g, ''),
    			kftc_bkpr: item.kftc_bkpr?.replace(/,/g, ''),
    			edate: date,
    		}));
    	}
    }