Get a list of Cities within country
GET https://api.countrystatecity.in/v1/countries/[ciso]/cities
Security
This api use API KEY as an authentication method.
- Name: X-CSCAPI-KEY
- In: header
Request Parameters
Name | Located in | Description | Required | Type |
---|---|---|---|---|
[ciso] | URL | ISO2 Code of Country | Required | string |
Response
Code | Description |
---|---|
200 | Return a list of cities |
401 | Unauthorized. |
404 | Not Found. |
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/IN/cities", 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/IN/cities',
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/IN/cities"
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/IN/cities',
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/IN/cities'));
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/IN/cities",
"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/IN/cities"
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/IN/cities')
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)
cities_in_country = JSON.parse(response.body)
p cities_in_country
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/IN/cities"),
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/IN/cities"))
.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/IN/cities")! 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": 133024,
"name": "Mumbai",
},
...
]
Example Error Response
{
"error'": "Unauthorized. You shouldn't be here."
}
Example Not Found Response
{
"error'": "No Cities found."
}