Learn/เริ่มต้นใช้ Python

วิธีรับอัตราแลกเปลี่ยนด้วย Python

เริ่มจาก standard library ใช้ requests เมื่อต้องการควบคุมมากขึ้น และใช้ forex-python เป็นขั้นตอนระหว่างการย้ายระบบได้

ERexchangerate.dev·อัปเดต 1 ตุลาคม 2026·อ่าน 6 นาที

/v1/latest/USD คืน JSON ที่มีอัตรา แหล่งข้อมูล และเวลาที่สังเกต ใช้ Decimal เมื่อคำนวณจำนวนเงิน

ประเด็นสำคัญ
urllib ไม่ต้องติดตั้ง dependency เพิ่ม
requests ช่วยจัดการ authentication, timeout และ error
forex-python ใช้เป็น interface ชั่วคราวระหว่างย้ายระบบได้
เก็บ source, effective_at และ data_updated_at คู่กับค่า
ใช้ endpoint วันที่หรือ /v1/range สำหรับข้อมูลย้อนหลัง

คำขอแรก

standard library เพียงพอสำหรับคำขอแรก ถ้าคำนวณเงินให้อ่านตัวเลข JSON เป็น Decimal

ใช้ requests กับ key

ใน service ให้ตั้ง timeout อ่าน key จาก environment และตรวจ HTTP status ก่อนบันทึก

ย้ายจาก forex-python

forex-python ใช้ผู้ให้บริการของตัวเองและไม่ได้เชื่อมต่อกับ exchangerate.dev โดยอัตโนมัติ ระหว่างย้ายระบบเก็บ interface เดิมได้ แต่อย่าทิ้ง source และเวลาที่สังเกตจาก exchangerate.dev

จัดการข้อมูลย้อนหลังให้ถูกต้อง

endpoint วันที่อาจคืนค่าที่ต่อเนื่องจากวันก่อนเมื่อไม่มีการเผยแพร่ใหม่ ส่วน /v1/range คืน observation ที่บันทึกไว้และแบ่ง JSON หน้าละ 366 แถว

คำถามที่พบบ่อย

python · standard librarycopy
import json
from decimal import Decimal
import urllib.request

url = "https://api.exchangerate.dev/v1/latest/USD?symbols=EUR,GBP"
with urllib.request.urlopen(url, timeout=10) as response:
    data = json.load(response, parse_float=Decimal)

print(data["rates"]["EUR"])
print(data["sources"]["EUR"], data["effective_at"]["EUR"])
print(data["market_session"])
python · requestscopy
# Install first: python -m pip install requests
import os
from decimal import Decimal
import requests

response = requests.get(
    "https://api.exchangerate.dev/v1/latest/USD",
    headers={"Authorization": f"Bearer {os.environ['EXCHANGERATE_API_KEY']}"},
    timeout=10,
    allow_redirects=False,
)
if response.status_code == 429:
    raise RuntimeError(f"Rate limited; Retry-After: {response.headers.get('Retry-After')}")
if 300 <= response.status_code < 400:
    raise RuntimeError("Unexpected redirect")
response.raise_for_status()
data = response.json(parse_float=Decimal)
print(data["rates"]["GBP"], data["effective_at"]["GBP"])
python · convert 100 USD to EURcopy
import os
from decimal import Decimal
import requests

response = requests.get(
    "https://api.exchangerate.dev/v1/convert/USD/EUR/100",
    headers={"Authorization": f"Bearer {os.environ['EXCHANGERATE_API_KEY']}"},
    timeout=10,
    allow_redirects=False,
)
if not 200 <= response.status_code < 300:
    raise RuntimeError(f"HTTP {response.status_code}; Retry-After: {response.headers.get('Retry-After')}")
data = response.json(parse_float=Decimal)
print(data["rate"], data["converted"])
python · rate on a past datecopy
import os
from decimal import Decimal
import requests

response = requests.get(
    "https://api.exchangerate.dev/v1/2026-03-14/USD?symbols=JPY",
    headers={"Authorization": f"Bearer {os.environ['EXCHANGERATE_API_KEY']}"},
    timeout=10,
    allow_redirects=False,
)
if not 200 <= response.status_code < 300:
    raise RuntimeError(f"HTTP {response.status_code}; Retry-After: {response.headers.get('Retry-After')}")
data = response.json(parse_float=Decimal)
print(data["rates"]["JPY"], data["is_forward_filled"])
ER
exchangerate.dev
คู่มือการเชื่อมต่อสำหรับนักพัฒนา

อ่านต่อ

Tutorialแปลงสกุลเงินด้วย JavaScriptอ่าน Tutorialอัตราย้อนหลังและอนุกรมเวลาอ่าน
การเปรียบเทียบFixer vs exchangerate.devการเปรียบเทียบOpen Exchange Rates vs exchangerate.devการเปรียบเทียบCurrencylayer vs exchangerate.devคู่มือแปลงสกุลเงินด้วย JavaScript และ Node.jsคู่มืออัตราแลกเปลี่ยนใน Next.js และ TypeScriptคู่มือสร้างการแจ้งเตือนอัตรา USD/IDR ด้วย Pythonอัตราแลกเปลี่ยนสดEUR/USDอัตราแลกเปลี่ยนสดGBP/USDอัตราแลกเปลี่ยนสดUSD/JPY