The Random Country API is API that allows you to retrieve information about a randomly selected country.
The API returns a JSON response containing the following information about a randomly selected country:
- name: The name of the country.
- alpha_2: The two-letter country code (alpha-2 code) assigned to the country.
- alpha_3: The three-letter country code (alpha-3 code) assigned to the country.
- numeric: The three-digit country code (numeric code) assigned to the country.
URL: https://api.apipip.com/v1/random-country/
Parameters
Name | Description |
---|
Headers
Name | Description |
---|---|
X-Key |
(No required) |
How to use Random Country Api
import requests
# Make a GET request to the Random Country API
response = requests.get('https://api.apipip.com/v1/random-country/')
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the response as JSON
data = response.json()
print("Random Country Information:")
print("Name:", data['name'])
print("Alpha-2 Code:", data['alpha_2'])
print("Alpha-3 Code:", data['alpha_3'])
print("Numeric Code:", data['numeric'])
else:
print("Error:", response.status_code)
fetch('https://api.apipip.com/v1/random-country/')
.then(response => response.json())
.then(data => {
console.log("Random Country Information:");
console.log("Name:", data.name);
console.log("Alpha-2 Code:", data.alpha_2);
console.log("Alpha-3 Code:", data.alpha_3);
console.log("Numeric Code:", data.numeric);
})
.catch(error => console.log("Error:", error));
import Foundation
guard let url = URL(string: "https://api.apipip.com/v1/random-country/") else {
fatalError("Invalid URL")
}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error {
print("Error:", error)
return
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
if let countryData = json as? [String: Any] {
print("Random Country Information:")
print("Name:", countryData["name"] ?? "")
print("Alpha-2 Code:", countryData["alpha_2"] ?? "")
print("Alpha-3 Code:", countryData["alpha_3"] ?? "")
print("Numeric Code:", countryData["numeric"] ?? "")
}
} catch {
print("Error parsing JSON:", error)
}
}
}
task.resume()
API Response
{
"name": "United States",
"alpha_2": "US",
"alpha_3": "USA",
"numeric": "840"
}
Note: The API works with all programming languages