Using the REST Countries API from Python in 2026
The old keyless REST Countries URLs (restcountries.com/v3.1/...) now answer with a deprecation message, and the current v5 API needs a bearer key from a free account. If your Python script broke, either move to v5 with a key, use the offline pycountry package for ISO codes, or call a country lookup API that needs no account. Our Country Info API returns ISO codes, capital, currency, calling code, languages and flag for 249 countries at $0.002 per call.
Try it free Country Info API — ISO codes, currency, calling code, $0.002 · $0.002 / call
What broke, and why
For years the standard Python snippet was requests.get("https://restcountries.com/v3.1/name/germany"). REST Countries has since moved to a v5 API at api.restcountries.com with a bearer token, and the v3.1 endpoints return an error saying the version is deprecated (restcountries.com docs, checked December 2026). Old tutorials and Stack Overflow answers still show v3.1, which is why so many scripts fail with the same message.
Your options in Python
| Option | Key or account | What you get | Watch out for |
|---|---|---|---|
| REST Countries v5 | Free account, bearer key | The richest data set, maintained | New URL shape; free tier limits not published on the docs page |
| pycountry (pip package) | None, works offline | ISO 3166 codes and names, ISO 4217 currencies, languages | No capitals, calling codes or regions; you join tables yourself |
| A static JSON file in your repo | None | Exactly the fields you choose | You maintain it when codes change |
| Country Info API (iamai.markets) | None; pay per call with x402 | Codes, capital, region, currency, calling code, languages, domain, flag, driving side | $0.002 per call; English names only |
A working call in Python
The request body is one field. Codes ("DE", "DEU", "276") and English names both work, and so do common names and small typos.
import requests
API = "https://iamai.markets/api/x402/services/UYkFcIVfKUEJ"
# A plain POST shows the price: HTTP 402, $0.002.
print(requests.post(API, json={"country": "Germany"}).status_code) # 402
# pay_session: a requests session wrapped by your x402 v2 client
c = pay_session.post(API, json={"country": "Germany"}, timeout=20).json()["result"]["country"]
print(c["iso2"], c["iso3"], c["capital"], c["currency"]["code"], c["calling_code"], c["flag"])
# Every country in a region in one call
rows = pay_session.post(API, json={"region": "Western Europe"}).json()["result"]["countries"]
Matching messy country names
Real data has "UK", "Viet Nam", "Germnay" and "the Netherlands" in it. The API tries an exact code, then exact names and aliases, then names that start with or contain your text, then a spelling correction. The answer says which one happened in matched_by and lists other close matches in alternatives, so you can flag a row instead of trusting a guess.
The data is built into the API, so there is no outside service in the path. Kosovo has no ISO 3166-1 code and is not included, and capitals of a few disputed places are given as commonly listed.
The product
Questions
Is REST Countries still free?
It has a free tier, but v5 needs an account and a bearer key. The keyless v3.1 URLs no longer return country data.
Can I use pycountry instead?
Yes, for ISO codes, names and currencies. It has no capitals or calling codes, so add those from another source.
Can I try your country API without a wallet?
Yes. The listing page gives 5 free tries a day with your own request.
Related
Updated 2026-12-02
