Babelfish translation service deployed as Vercel serverless functions. Provides language detection and translation services with Google Spreadsheet caching.
Detect the language of a text query.
Parameters:
query(string, required) - Text to detect language for
Example:
curl "https://your-deployment.vercel.app/api/detect-language?query=hello"Response:
{
"ok": 1,
"language": "en",
"confidence": 1,
"alternate": null,
"name": "English"
}Translate text from one language to another with caching.
Parameters:
query(string, required) - Text to translatelangFrom(string, required) - Source language code (e.g., "en", "zh-CN")langTo(string, required) - Target language code (e.g., "en", "zh-CN")secret(string, required) - Shared secret for authenticationlangConfidence(number, optional) - Language detection confidencelangAlternate(string, optional) - Alternate language suggestionlangName(string, optional) - Human-readable language name
Example:
curl -X POST https://your-deployment.vercel.app/api/translate \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "query=hello&langFrom=en&langTo=zh-CN&secret=your_secret"Response:
{
"ok": 1,
"query": "hello",
"langFrom": "en",
"langTo": "zh-CN",
"translated": "你好",
"sensitive": false,
"saved": true,
"source": "google"
}npm installCopy .env.example to .env and fill in your credentials:
cp .env.example .envRequired variables:
GOOGLE_TRANSLATE_API_KEY- Your Google Translate API keySHARED_SECRET- Secret key for authenticating translation requests
Optional variables (for caching):
GOOGLE_SPREADSHEET_ID- Google Sheet ID for caching translationsGOOGLE_SERVICE_ACCOUNT_KEY- Service account credentials as JSON string
Start the local development server:
npm run devThe API will be available at http://localhost:3002
Test language detection:
curl "http://localhost:3002/api/detect-language?query=hello"Test translation:
curl -X POST http://localhost:3002/api/translate \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "query=hello&langFrom=en&langTo=zh-CN&secret=your_secret"npm run deployOr connect your GitHub repository to Vercel for automatic deployments.
After deploying, configure the following environment variables in your Vercel project settings:
- Go to your project on Vercel
- Navigate to Settings → Environment Variables
- Add each variable from
.env.example
Translations are cached in a Google Spreadsheet with three priority levels:
- Sensitive terms (highest priority) - Special terms with manual translations
- Override translations - Manually corrected translations
- Cached Google translations - Previously fetched translations
If no cache exists, the API fetches from Google Translate and saves to the spreadsheet.
The /api/translate endpoint requires a shared secret passed as the secret parameter. This prevents unauthorized usage and API quota consumption.
The /api/detect-language endpoint does not require authentication and can be called freely. It returns the detected language code, confidence level, and human-readable language name.
This serverless version replaces the original Node.js HTTP/HTTPS server with Vercel functions. Key differences:
- No Socket.io - Real-time features removed (not suitable for serverless)
- On-demand caching - Spreadsheet loaded per request with 5-minute cache
- Simplified authentication - Uses shared secret only (no service account validation)
- Removed endpoints -
/query,/submit-images,/imagesnot included
translate-api/
├── api/ # Serverless function endpoints
├── lib/ # Shared utilities and services
├── package.json # Dependencies and scripts
├── vercel.json # Vercel deployment config
└── local-server.js # Local development server
- Create a new file in
/api/(e.g.,/api/new-endpoint.js) - Export a handler function wrapped with
allowCors() - Add route to
local-server.jsfor local testing
Part of the Firewall Cafe project.