This API returns the capital from a given country name.
URL: https://api.apipip.com/v1/capital/
Parameters
Name | Description |
---|---|
country_name |
(Required) the name of the country for which you want to retrieve the capital. |
Headers
Name | Description |
---|---|
X-Key |
(No required) |
How to use Get Capital From Country API
import requests
# Define the API endpoint URL
url = 'https://api.apipip.com/v1/capital/'
# Define the country data to send in the request
data = {
'country_name': 'France'
}
# Send a POST request to the API endpoint
response = requests.post(url, data=data)
print(response.text)
const url = 'https://api.apipip.com/v1/capital/';
const data = {
country_name: 'France'
};
fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Request failed:', error));
import Foundation
let url = URL(string: "https://api.apipip.com/v1/capital/")!
let countryName = "France"
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let requestData = ["country_name": countryName]
request.httpBody = try? JSONSerialization.data(withJSONObject: requestData)
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Request failed:", error)
} else if let data = data {
if let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
print(json)
} else {
print("Invalid JSON response")
}
}
}
task.resume()
API Response
{"capital":"Paris"}
Note: The API works with all programming languages