Menu

Currency API in Python, Go and PHP: The Same Exchange Rate Call, Done Properly in Each
📰

Currency API in Python, Go and PHP: The Same Exchange Rate Call, Done Properly in Each

DEV Community: php·chathuranga basnayaka·5 days ago
#3JaAtUVa
#devto#dev#time#decimal#rates
Long article (1422 words) — no AI summary yet.
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.