@countrydataapi/sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,222 @@
1
+ # Country Data API - TypeScript/JavaScript SDK
2
+
3
+ Official SDK for the [Country Data API](https://countrydataapi.com), providing easy access to geographical data including countries, states, cities, and postal codes worldwide.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @countrydataapi/sdk
9
+ # or
10
+ yarn add @countrydataapi/sdk
11
+ # or
12
+ pnpm add @countrydataapi/sdk
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```typescript
18
+ import { CountryDataApi } from '@countrydataapi/sdk';
19
+
20
+ const api = new CountryDataApi({
21
+ apiKey: 'your-api-key'
22
+ });
23
+
24
+ // Get all countries
25
+ const countries = await api.countries.getAll({ lang: 'en' });
26
+ console.log(countries.data);
27
+
28
+ // Get country by ISO code
29
+ const spain = await api.countries.getByCode({ code: 'ES' });
30
+ console.log(spain.data[0].country_name); // "Spain"
31
+
32
+ // Get states for a country
33
+ const states = await api.states.getByCountry({ country: 'Spain' });
34
+ console.log(states.data);
35
+
36
+ // Get cities for a state
37
+ const cities = await api.cities.getByState({ state: 'Madrid' });
38
+ console.log(cities.data);
39
+
40
+ // Check remaining tokens
41
+ const status = await api.getStatus();
42
+ console.log(`Remaining tokens: ${status.remainingTokens}`);
43
+ ```
44
+
45
+ ## Configuration
46
+
47
+ ```typescript
48
+ const api = new CountryDataApi({
49
+ apiKey: 'your-api-key', // Required
50
+ baseUrl: 'https://api.countrydataapi.com', // Optional
51
+ timeout: 30000, // Optional (ms)
52
+ defaultLanguage: 'en' // Optional
53
+ });
54
+ ```
55
+
56
+ ## API Reference
57
+
58
+ ### Countries
59
+
60
+ ```typescript
61
+ // Get all countries
62
+ api.countries.getAll(options?)
63
+
64
+ // Get country by name
65
+ api.countries.getByName({ name: 'Spain', ...options })
66
+
67
+ // Get country by ISO code (alpha-2, alpha-3, numeric, or CIOC)
68
+ api.countries.getByCode({ code: 'ES', ...options })
69
+
70
+ // Get countries by region
71
+ api.countries.getByRegion({ region: 'Europe', ...options })
72
+
73
+ // Get countries by currency
74
+ api.countries.getByCurrency({ currency: 'EUR', ...options })
75
+
76
+ // Get countries by language
77
+ api.countries.getByLanguage({ language: 'spa', ...options })
78
+
79
+ // Get countries by timezone
80
+ api.countries.getByTimezone({ timezone: 'Europe/Madrid', ...options })
81
+ ```
82
+
83
+ ### States
84
+
85
+ ```typescript
86
+ // Get all states
87
+ api.states.getAll(options?)
88
+
89
+ // Get states by country
90
+ api.states.getByCountry({ country: 'Spain', ...options })
91
+
92
+ // Get states by city
93
+ api.states.getByCity({ city: 'Madrid', ...options })
94
+
95
+ // Get states by zipcode
96
+ api.states.getByZipcode({ zipcode: '28001', ...options })
97
+ ```
98
+
99
+ ### Cities
100
+
101
+ ```typescript
102
+ // Get all cities
103
+ api.cities.getAll(options?)
104
+
105
+ // Get city by name
106
+ api.cities.get({ city: 'Madrid', ...options })
107
+
108
+ // Get cities by country
109
+ api.cities.getByCountry({ country: 'Spain', ...options })
110
+
111
+ // Get cities by state
112
+ api.cities.getByState({ state: 'Madrid', ...options })
113
+ ```
114
+
115
+ ### Zipcodes
116
+
117
+ ```typescript
118
+ // Get all zipcodes
119
+ api.zipcodes.getAll(options?)
120
+
121
+ // Get zipcodes by country
122
+ api.zipcodes.getByCountry({ country: 'Spain', ...options })
123
+
124
+ // Get zipcodes by state
125
+ api.zipcodes.getByState({ state: 'Madrid', ...options })
126
+
127
+ // Get zipcodes by region
128
+ api.zipcodes.getByRegion({ region: 'Europe', ...options })
129
+ ```
130
+
131
+ ### Select (Optimized for Dropdowns)
132
+
133
+ These endpoints cost only **1 token** regardless of the number of results, making them perfect for populating dropdowns.
134
+
135
+ ```typescript
136
+ // Get countries for select
137
+ api.select.countries({ lang: 'es' })
138
+
139
+ // Get states for select
140
+ api.select.states({ country: 'ES', lang: 'es' })
141
+
142
+ // Get cities for select
143
+ api.select.cities({ state: 'Madrid', country: 'ES', lang: 'es' })
144
+ ```
145
+
146
+ ## Request Options
147
+
148
+ All methods accept these common options:
149
+
150
+ | Option | Type | Description |
151
+ |--------|------|-------------|
152
+ | `fields` | `string` | Comma-separated list of fields to return |
153
+ | `lang` | `'en' \| 'es' \| 'pt' \| 'fr' \| 'de' \| 'it'` | Response language |
154
+ | `limit` | `number` | Maximum number of items to return |
155
+ | `limitToken` | `number` | Maximum tokens to use for this request |
156
+
157
+ ## Response Format
158
+
159
+ All methods return an `ApiResponse<T>` object:
160
+
161
+ ```typescript
162
+ interface ApiResponse<T> {
163
+ success: boolean;
164
+ data: T[];
165
+ count: number;
166
+ tokens_used: number;
167
+ remaining_tokens?: number;
168
+ }
169
+ ```
170
+
171
+ ## Error Handling
172
+
173
+ ```typescript
174
+ import {
175
+ CountryDataApi,
176
+ AuthenticationError,
177
+ InsufficientTokensError
178
+ } from '@countrydataapi/sdk';
179
+
180
+ try {
181
+ const countries = await api.countries.getAll();
182
+ } catch (error) {
183
+ if (error instanceof AuthenticationError) {
184
+ console.error('Invalid API key');
185
+ } else if (error instanceof InsufficientTokensError) {
186
+ console.error('No tokens remaining');
187
+ } else {
188
+ console.error('API error:', error.message);
189
+ }
190
+ }
191
+ ```
192
+
193
+ ## TypeScript Support
194
+
195
+ This SDK is written in TypeScript and provides full type definitions:
196
+
197
+ ```typescript
198
+ import type {
199
+ Country,
200
+ State,
201
+ City,
202
+ ApiResponse,
203
+ Language
204
+ } from '@countrydataapi/sdk';
205
+
206
+ const response: ApiResponse<Country> = await api.countries.getByCode({
207
+ code: 'ES'
208
+ });
209
+
210
+ const country: Country = response.data[0];
211
+ console.log(country.country_name);
212
+ ```
213
+
214
+ ## License
215
+
216
+ MIT
217
+
218
+ ## Links
219
+
220
+ - [Documentation](https://countrydataapi.com/docs)
221
+ - [API Reference](https://countrydataapi.com/docs/api-reference)
222
+ - [Get API Key](https://countrydataapi.com/pricing)