Get a list of Countries
GET https://api.countrystatecity.in/v1/countries
Security
This api use API KEY as an authentication method.
- Name: X-CSCAPI-KEY
- In: header
Request Parameters
No parameters
Response
Code | Description |
---|---|
200 | Return a list of countries |
401 | Unauthorized. |
Example Usage
- Javascript
- PHP
- Python
- Axios
- Dart
- jQuery
- Go
- Ruby
- C#
- Java
- Swift
countries-states-cities.js
var headers = new Headers();
headers.append("X-CSCAPI-KEY", "API_KEY");
var requestOptions = {
method: 'GET',
headers: headers,
redirect: 'follow'
};
fetch("https://api.countrystatecity.in/v1/countries", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
countries-states-cities.php
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.countrystatecity.in/v1/countries',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'X-CSCAPI-KEY: API_KEY'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
countries-states-cities.py
import requests
url = "https://api.countrystatecity.in/v1/countries"
headers = {
'X-CSCAPI-KEY': 'API_KEY'
}
response = requests.request("GET", url, headers=headers)
print(response.text)
countries-states-cities.js
var axios = require('axios');
var config = {
method: 'get',
url: 'https://api.countrystatecity.in/v1/countries',
headers: {
'X-CSCAPI-KEY': 'API_KEY'
}
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
countries-states-cities.dart
var headers = {
'X-CSCAPI-KEY': 'API_KEY'
};
var request = http.Request('GET', Uri.parse('https://api.countrystatecity.in/v1/countries'));
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
print(await response.stream.bytesToString());
} else {
print(response.reasonPhrase);
}
countries-states-cities.js
var settings = {
"url": "https://api.countrystatecity.in/v1/countries",
"method": "GET",
"headers": {
"X-CSCAPI-KEY": "API_KEY"
},
};
$.ajax(settings).done(function (response) {
console.log(response);
});
countries-states-cities.go
url:="https://api.countrystatecity.in/v1/countries"
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("X-CSCAPI-KEY", "API_KEY")
res, _ := client.Do(req)
defer res.Body.Close()
bytes ,err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(bytes))
countries-states-cities.ruby
require 'net/http'
require 'json'
uri = URI('https://api.countrystatecity.in/v1/countries')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri)
request['X-CSCAPI-KEY'] = 'API_KEY'
response = http.request(request)
countries = JSON.parse(response.body)
p countries
countries-states-cities.cs
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://api.countrystatecity.in/v1/countries"),
Headers =
{
{ "X-CSCAPI-KEY", "API_KEY" },
},
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
}
}
countries-states-cities.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.countrystatecity.in/v1/countries"))
.header("X-CSCAPI-KEY", "API_KEY")
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
countries-states-cities.swift
import Foundation
let headers = [
"X-CSCAPI-KEY": "API_KEY"
]
let request = NSMutableURLRequest(url: NSURL(string: "https://api.countrystatecity.in/v1/countries")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()
Example Success Response
[
{
"id": 101,
"name": "India",
"iso2": "IN",
"iso3": "IND",
"phonecode": "91",
"capital": "New Delhi",
"currency": "INR",
"native": "भारत",
"emoji": "🇮🇳"
},
...
]
Example Error Response
{
"error'": "Unauthorized. You shouldn't be here."
}