# Language Code API
This project is a simple, fast, and efficient Node.js API for looking up language information. It allows you to query language details using ISO 639-1 (2-letter), ISO 639-2 (3-letter), or the language's name in either English or its native form.
The API is built with Express.js and is configured for easy deployment on serverless platforms like Vercel.
## Features
- **Flexible Search**: Find languages by ISO 639-1 code, ISO 639-2 code, or name.
- **Comprehensive Data**: Returns the language's English name, native name, and both ISO 639-1 and ISO 639-2 codes.
- **Multi-variant Name Support**: Correctly handles and indexes languages with multiple names (e.g., "Catalan; Valencian").
- **Efficient**: Pre-builds an index on startup for fast lookups.
- **Serverless-Ready**: Includes a `vercel.json` for seamless deployment.
---
## API Endpoint
The API provides a single, powerful endpoint to handle all queries.
`GET /api/language`
### Query Parameters
You must provide one of the following query parameters:
| Parameter | Type | Description | Example |
| :-------- | :----- |:---------------------------------------------------------------------------------------------------------| :---------------------- |
| `code` | string | An ISO 639-1 (2-letter) or ISO 639-2 (3-letter, either bibliographical or terminological) language code. | `?code=es` or `?code=spa` |
| `name` | string | The language name in English or its native form. Search is case-insensitive. | `?name=Spanish` or `?name=Español` |
### Success Response (200 OK)
If a language is found, the API returns a JSON object with all its details.
```json
{
"iso639_1": "es",
"english": "Spanish",
"native": "Español",
"iso639_2_b": "spa",
"iso639_2_t": "spa"
}
```
### Error Response (404 Not Found)
If no language matches the query, the API returns a 404 error.
```json
{
"error": "Language not found."
}
```
---
## Usage Examples
You can test the API using `curl` or by simply visiting the URLs in your browser.
**1. Search by ISO 639-1 code:**
```bash
curl "https:///api/language?code=de"
```
**2. Search by ISO 639-2 code:**
```bash
curl "https:///api/language?code=deu"
```
**3. Search by English name:**
```bash
curl "https:///api/language?name=German"
```
**4. Search by a variant of a native name:**
```bash
# For a language with native name "Аҧсуа"
curl "https:///api/language?name=Аҧсуа"
```
---
## Deployment
### Running Locally
To run this API on your local machine, you'll need [Node.js](https://nodejs.org/) and `npm` installed.
1. **Clone the repository:**
```bash
git clone
cd
```
2. **Install dependencies:**
The project uses Express.js.
```bash
npm install express
```
3. **Run the server:**
```bash
node api/translate.js
```
4. **Access the API:**
The server will start on port 3000 by default. You can now make requests to `http://localhost:3000/api/language`.
### Deploying to Vercel
This project is optimized for deployment on [Vercel](https://vercel.com/).
1. **Sign up and Install CLI:**
Create a Vercel account and (optionally) install the Vercel CLI:
```bash
npm i -g vercel
```
2. **Connect to Git:**
Push your project to a GitHub, GitLab, or Bitbucket repository.
3. **Import and Deploy:**
- Log in to your Vercel dashboard and import the Git repository.
- Vercel will automatically detect the `vercel.json` file and configure the project as a Node.js serverless function.
- Click **Deploy**. Vercel will build and deploy your API, providing you with a public URL.
The `vercel.json` file handles the routing, ensuring that requests to `/api/language` are correctly directed to the `api/translate.js` serverless function.
---
## Data Format
The language data is stored in `api/languages.json`. The file uses the ISO 639-1 (2-letter) code as the key for each language object.
```json name=api/languages.json
{
"en": {
"english": "English",
"native": "English",
"iso639_2_b": "eng",
"iso639_2_t": "eng"
},
"ca": {
"english": "Catalan; Valencian",
"native": "Català",
"iso639_2_b": "cat",
"iso639_2_t": "cat"
}
}
```
You can easily extend the API by adding more language objects to this file.