The /v1/select/countries endpoint returns a lightweight list of all countries with only id and name fields, optimized for dropdown menus and form inputs.
GET https://api.countrydataapi.com/v1/select/countries
Include your API key as a query parameter:
?apikey=your-api-key
Your API key is like a password, keep it secure. Get your key from the account dashboard.
| Parameter | Type | Required | Description |
|---|---|---|---|
apikey |
string | Yes | Your API authentication key |
lang |
string | No | Language code for country names (default: en) |
en - English (default)es - Spanishpt - Portuguesefr - Frenchde - Germanit - Italiancurl "https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=en"
const API_KEY = 'your-api-key';
async function loadCountries() {
const response = await fetch(
`https://api.countrydataapi.com/v1/select/countries?apikey=${API_KEY}&lang=en`
);
const data = await response.json();
return data;
}
import axios from 'axios';
const response = await axios.get(
'https://api.countrydataapi.com/v1/select/countries',
{
params: {
apikey: 'your-api-key',
lang: 'en',
},
}
);
import requests
response = requests.get(
'https://api.countrydataapi.com/v1/select/countries',
params={
'apikey': 'your-api-key',
'lang': 'en'
}
)
data = response.json()
<?php
$apiKey = 'your-api-key';
$url = "https://api.countrydataapi.com/v1/select/countries?apikey={$apiKey}&lang=en";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
?>
// country.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
interface Country {
id: string;
name: string;
}
interface ApiResponse {
success: boolean;
data: Country[];
}
@Injectable({
providedIn: 'root'
})
export class CountryService {
private readonly API_KEY = 'your-api-key';
private readonly BASE_URL = 'https://api.countrydataapi.com/v1';
constructor(private http: HttpClient) {}
getCountries(lang: string = 'en'): Observable<Country[]> {
const params = new HttpParams()
.set('apikey', this.API_KEY)
.set('lang', lang);
return this.http.get<ApiResponse>(
`${this.BASE_URL}/select/countries`,
{ params }
).pipe(
map(response => response.data)
);
}
}
// country-select.component.ts
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { CountryService } from './country.service';
@Component({
selector: 'app-country-select',
standalone: true,
imports: [CommonModule, FormsModule],
template: `
<div *ngIf="loading">Loading countries...</div>
<div *ngIf="error" class="error">{{ error }}</div>
<select
*ngIf="!loading && !error"
[(ngModel)]="selectedCountry"
(ngModelChange)="onCountryChange($event)"
>
<option value="">Select a country...</option>
<option *ngFor="let country of countries" [value]="country.id">
{{ country.name }}
</option>
</select>
`
})
export class CountrySelectComponent implements OnInit {
countries: any[] = [];
selectedCountry = '';
loading = true;
error: string | null = null;
constructor(private countryService: CountryService) {}
ngOnInit() {
this.countryService.getCountries('en').subscribe({
next: (data) => {
this.countries = data;
this.loading = false;
},
error: (err) => {
this.error = 'Failed to load countries';
this.loading = false;
}
});
}
onCountryChange(countryId: string) {
console.log('Selected country:', countryId);
// Trigger state loading, emit event, etc.
}
}
{
"success": true,
"data": [
{
"id": "66c7a6c9e4bda21f4ab10ef2",
"name": "Afghanistan"
},
{
"id": "66c7a6c9e4bda21f4ab10ef3",
"name": "Albania"
},
{
"id": "66c7a6c9e4bda21f4ab10ef4",
"name": "Algeria"
}
// ... more countries (200+ total)
]
}
| Field | Type | Description |
|---|---|---|
success |
boolean | Indicates if the request was successful |
data |
array | Array of country objects |
data[].id |
string | Unique country identifier (MongoDB ObjectId) |
data[].name |
string | Country name in the requested language |
{
"success": false,
"error": {
"code": "INVALID_API_KEY",
"message": "The provided API key is invalid"
}
}
See the Error Codes documentation for all possible error codes.
The select endpoint is optimized for minimal bandwidth:
/v1/countries/all): ~500KB (includes all country data)/v1/select/countries): ~15KB (only id and name)This makes the select endpoint 97% smaller and perfect for form dropdowns!
This endpoint consumes 1 token per request.
Tip: Cache the response in localStorage or your application state. Countries rarely change, so you can safely cache for 7+ days.
<select id="country">
<option value="">Select country...</option>
<!-- Populated via JavaScript -->
</select>
<script>
async function populateCountries() {
const response = await fetch(
'https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=en'
);
const { data } = await response.json();
const select = document.getElementById('country');
data.forEach(country => {
const option = new Option(country.name, country.id);
select.add(option);
});
}
populateCountries();
</script>
import { useState, useEffect } from 'react';
function CountrySelect() {
const [countries, setCountries] = useState([]);
useEffect(() => {
fetch('https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=en')
.then(res => res.json())
.then(({ data }) => setCountries(data));
}, []);
return (
<select>
<option value="">Select country...</option>
{countries.map(country => (
<option key={country.id} value={country.id}>
{country.name}
</option>
))}
</select>
);
}
<script setup>
import { ref, onMounted } from 'vue';
const countries = ref([]);
onMounted(async () => {
const response = await fetch(
'https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=en'
);
const { data } = await response.json();
countries.value = data;
});
</script>
<template>
<select>
<option value="">Select country...</option>
<option v-for="country in countries" :key="country.id" :value="country.id">
{{ country.name }}
</option>
</select>
</template>
Cache countries in localStorage to minimize API calls:
const CACHE_KEY = 'countries_cache';
const CACHE_DURATION = 7 * 24 * 60 * 60 * 1000; // 7 days
async function getCountries() {
// Check cache first
const cached = localStorage.getItem(CACHE_KEY);
if (cached) {
const { data, timestamp } = JSON.parse(cached);
if (Date.now() - timestamp < CACHE_DURATION) {
return data;
}
}
// Fetch from API
const response = await fetch(
'https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=en'
);
const { data } = await response.json();
// Save to cache
localStorage.setItem(CACHE_KEY, JSON.stringify({
data,
timestamp: Date.now()
}));
return data;
}
Switch between languages dynamically:
function loadCountries(language = 'en') {
return fetch(
`https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=${language}`
).then(res => res.json());
}
// Load in Spanish
const spanishCountries = await loadCountries('es');
// Load in French
const frenchCountries = await loadCountries('fr');
Check our pricing page for more details.
Note: The
idfield returned is a MongoDB ObjectId that uniquely identifies each country across all endpoints. Use this ID when querying states, cities, or other related data.