A comprehensive Node.js/TypeScript API service that provides multiple methods to search for Indian legal cases and retrieve their URLs from the Indian Kanoon database. The service integrates with various search engines and AI models to provide accurate case lookup functionality.
-
Multiple Search Methods:
- LLM-powered search using OpenAI GPT-4
- Google Custom Search Engine integration
- SerpAPI Google search integration
- Direct Indian Kanoon API integration
- Python-based advanced search with fuzzy matching
-
Intelligent Fallback System: Automatically tries different search methods if one fails
-
Fuzzy Matching: Uses difflib for intelligent case title matching
-
RESTful API: Clean and simple HTTP endpoints
-
TypeScript Support: Full type safety and modern JavaScript features
-
Cloud Ready: Optimized for deployment on Google Cloud Run
- Node.js 22 or higher
- Python 3.7 or higher
- npm or yarn package manager
- Google Cloud Platform account (for deployment)
- Firebase CLI (for deployment)
-
Clone the repository
git clone <repository-url> cd search_url_searching/test2
-
Install Node.js dependencies
npm install
-
Install Python dependencies
pip install -r requirements.txt
-
Set up environment variables Create a
.envfile in the root directory:# Required API Keys IK_TOKEN=your_indian_kanoon_api_token GEMINI_API_KEY=your_gemini_api_key SERPAPI_API_KEY=your_serpapi_key GOOGLE_API_KEY=your_google_custom_search_api_key SEARCH_ENGINE_ID=your_google_custom_search_engine_id # Optional PORT=3001
npm run startnpm run build
node dist/index.jsThe server will start on http://localhost:3001 (or the port specified in your environment variables).
POST /search
Searches for a case using the Python-based advanced search with fuzzy matching.
Request Body:
{
"title": "K.M. Nanavati v. State of Maharashtra"
}Response:
{
"input": "K.M. Nanavati v. State of Maharashtra",
"best_match": {
"title": "K.M. Nanavati v. State of Maharashtra",
"url": "https://indiankanoon.org/doc/1596139/"
},
"top_results": [
{
"title": "K.M. Nanavati v. State of Maharashtra",
"url": "https://indiankanoon.org/doc/1596139/"
}
]
}POST /find
Alternative search endpoint with enhanced error handling and logging.
Request Body:
{
"title": "Case title here"
}Response:
{
"input": "Case title here",
"best_match": {
"title": "Exact case title from database",
"url": "https://indiankanoon.org/doc/123456/"
},
"top_results": [...]
}- Uses OpenAI GPT-4 to intelligently search for cases
- Provides direct Indian Kanoon URLs
- High accuracy for well-known cases
- Uses SerpAPI to search Google for Indian Kanoon results
- Searches specifically within
indiankanoon.orgdomain - Good for finding cases with partial information
- Uses Google Custom Search Engine API
- Configured to search Indian Kanoon specifically
- Reliable and fast results
- Direct integration with Indian Kanoon's official API
- Multiple query variations for better matching
- Handles case title variations (vs, v., etc.)
- Uses the official Indian Kanoon Python API
- Implements fuzzy matching with difflib
- Provides best match and top results
- Most comprehensive search method
βββ dist/ # Compiled JavaScript files
βββ node_modules/ # Node.js dependencies
βββ output/ # Output directory for logs
βββ src/ # Source TypeScript files
β βββ index.ts # Main server file
β βββ googleSearch.ts # SerpAPI Google search
β βββ googleCustomSearch.ts # Google CSE search
β βββ indianKanoonApi.ts # Direct IK API integration
β βββ llmSearch.ts # OpenAI LLM search
βββ ikapi.py # Indian Kanoon Python API
βββ ikapi_modified_search.py # Modified Python search script
βββ package.json # Node.js dependencies and scripts
βββ tsconfig.json # TypeScript configuration
βββ README.md # This documentation
-
Install Firebase CLI
npm install -g firebase-tools
-
Login to Firebase
firebase login
-
Initialize Firebase in your project
firebase init functions
-
Update
package.json(already configured):{ "name": "functions", "engines": { "node": "22" }, "main": "index.js", "scripts": { "build": "tsc", "deploy": "firebase deploy --only functions" } } -
Create
firebase.jsonin the root directory:{ "functions": { "source": ".", "runtime": "nodejs22", "predeploy": ["npm run build"] } }
-
Create
Dockerfile:FROM node:22-slim # Install Python and pip RUN apt-get update && apt-get install -y python3 python3-pip && rm -rf /var/lib/apt/lists/* # Set working directory WORKDIR /app # Copy package files COPY package*.json ./ # Install Node.js dependencies RUN npm ci --only=production # Copy Python files COPY *.py ./ # Install Python dependencies RUN pip3 install --no-cache-dir -r requirements.txt # Copy TypeScript source COPY *.ts ./ COPY tsconfig.json ./ # Build TypeScript RUN npm run build # Expose port EXPOSE 8080 # Set environment variables ENV PORT=8080 # Start the application CMD ["node", "dist/index.js"]
-
Create
requirements.txt:requests>=2.25.0 -
Create
.dockerignore:node_modules npm-debug.log .git .gitignore README.md .env .env.local .env.development.local .env.test.local .env.production.local
- Build and push to Google Container Registry:
# Set your project ID export PROJECT_ID=your-gcp-project-id # Build the container gcloud builds submit --tag gcr.io/$PROJECT_ID/indian-kanoon-search # Deploy to Cloud Run gcloud run deploy indian-kanoon-search \ --image gcr.io/$PROJECT_ID/indian-kanoon-search \ --platform managed \ --region us-central1 \ --allow-unauthenticated \ --set-env-vars="IK_TOKEN=your_token,GEMINI_API_KEY=your_key,SERPAPI_API_KEY=your_key,GOOGLE_API_KEY=your_key,SEARCH_ENGINE_ID=your_id"
-
Convert to Firebase Functions format: Create
functions/index.ts:import * as functions from 'firebase-functions'; import express from 'express'; import cors from 'cors'; import { exec, spawn } from 'child_process'; const app = express(); app.use(cors()); app.use(express.json()); // Your existing endpoints here... export const api = functions.https.onRequest(app);
-
Deploy:
firebase deploy --only functions
| Variable | Description | Required |
|---|---|---|
IK_TOKEN |
Indian Kanoon API token | Yes |
GEMINI_API_KEY |
Google Gemini API key for LLM search | Yes |
SERPAPI_API_KEY |
SerpAPI key for Google search | Yes |
GOOGLE_API_KEY |
Google Custom Search API key | Yes |
SEARCH_ENGINE_ID |
Google Custom Search Engine ID | Yes |
PORT |
Server port (default: 3001) | No |
# Start the server
npm run start
# Test search endpoint
curl -X POST http://localhost:3001/search \
-H "Content-Type: application/json" \
-d '{"title": "K.M. Nanavati v. State of Maharashtra"}'
# Test find endpoint
curl -X POST http://localhost:3001/find \
-H "Content-Type: application/json" \
-d '{"title": "K.M. Nanavati v. State of Maharashtra"}'- Caching: Consider implementing Redis caching for frequently searched cases
- Rate Limiting: Implement rate limiting to prevent API abuse
- Monitoring: Set up Cloud Monitoring for production deployments
- Scaling: Cloud Run automatically scales based on traffic
npm run start- Start development server with hot reloadnpm run build- Build TypeScript to JavaScriptnpm run deploy- Deploy to Firebase Functionsnpm run lint- Run ESLint
- Create a new TypeScript file in the root directory
- Export a function that takes a title string and returns a URL or null
- Import and use it in
index.ts
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue in the repository
- Check the API documentation for Indian Kanoon
- Review the Google Cloud Run documentation
- Indian Kanoon API Documentation
- Google Cloud Run Documentation
- Firebase Functions Documentation
- Google Gemini API Documentation
- SerpAPI Documentation
Note: Make sure to keep your API keys secure and never commit them to version control. Use environment variables or a secure key management system for production deployments.