Most currency API snippets are one line: get(url).json()["rate"] . Production code needs a few more things in every language:
a timeout , so a slow network doesn't hang your request handler;
a retry on transient errors ( 5xx , timeouts), but not on 4xx ;
a cache , because rates update every 60 seconds and you don't need them more often;
exact decimals for money.
Here's that, in Python, Go and PHP, against AllRatesToday's API. All three need a free API key in ALLRATES_KEY .
The call is the same everywhere:
GET https://allratestoday.com/api/v1/rates?source = USD & target = EUR , GBP Authorization : Bearer art_live_ . . .
[ { "rate" : 0.914495 , "source" : "USD" , "target" : "EUR" , "time" : "..." } , { "rate" : 0.765706 , "source" : "USD" , "target" : "GBP" , "time" : "..." } ]
Enter fullscreen mode
Exit fullscreen mode
Python
import os , time from decimal import Decimal , ROUND_HALF_EVEN import requests from requests . adapters import HTTPAdapter from urllib3 . util .…