Skip to content

Utilizing Node.js and Express to create a server for enhanced security with hidden API keys, rate limiting, and efficient caching to optimize API performance.

Notifications You must be signed in to change notification settings

SaketKothari/api-proxy-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

17 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌐 Node API Proxy Server

A secure, production-ready proxy server for hiding API keys, implementing rate limiting, and caching responses. Built with Node.js and Express.

Node.js Express License

🎯 Features

  • πŸ” API Key Protection - Keep your API keys secure on the server side
  • ⚑ Rate Limiting - Prevent abuse with configurable request limits
  • πŸ’Ύ Response Caching - Improve performance and reduce API calls
  • 🌀️ Weather App Demo - Includes a beautiful weather application UI
  • πŸ”„ Flexible Configuration - Easy to adapt for any public API
  • 🎨 Modern UI - Responsive design with glassmorphism effects

πŸš€ Why Use an API Proxy?

When working with third-party APIs (OpenWeather, GitHub, Twitter, etc.), you typically need to include your API key in client-side requests. This exposes your credentials to anyone who inspects your code.

The Problem:

// ❌ API key exposed in client-side code
fetch(
  `https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY`
);

The Solution:

// βœ… API key hidden on server
fetch(`/api?q=London`);

This proxy server acts as a secure middleman, handling API authentication on the server while your client makes simple requests to your own endpoints.

πŸ“‹ Prerequisites

  • Node.js (v14 or higher)
  • npm or yarn
  • An API key from OpenWeather API (free tier available)

βš™οΈ Installation

  1. Clone the repository
git clone https://github.com/SaketKothari/api-proxy-server.git
cd api-proxy-server
  1. Install dependencies
npm install
  1. Configure environment variables

Rename .env.example to .env and add your API credentials:

API_BASE_URL=https://api.openweathermap.org/data/2.5/weather
API_KEY_NAME=appid
API_KEY_VALUE=your_openweather_api_key_here
  1. Start the development server
npm run dev
  1. Open your browser

Navigate to http://localhost:5000

πŸ”§ Configuration

Environment Variables

The .env file structure is designed to work with any public API:

# Base URL of the API (without query parameters)
API_BASE_URL=https://api.example.com/endpoint

# The query parameter name for the API key
API_KEY_NAME=apiKey

# Your actual API key value
API_KEY_VALUE=your_secret_key

Rate Limiting

Customize rate limits in your server configuration:

  • Default: 100 requests per 15 minutes per IP
  • Adjust as needed based on your API's limits

Caching

Configure cache duration to reduce redundant API calls:

  • Responses are cached to improve performance
  • Configurable TTL (Time To Live)

🎨 Usage Examples

Basic Weather Query

// Client-side request
fetch("/api?q=London")
  .then((res) => res.json())
  .then((data) => console.log(data));

With Additional Parameters

// Add any query parameters your API supports
fetch("/api?q=NewYork&units=metric")
  .then((res) => res.json())
  .then((data) => console.log(data));

πŸ“ Project Structure

api-proxy-server/
β”œβ”€β”€ index.js              # Main server file
β”œβ”€β”€ package.json          # Dependencies and scripts
β”œβ”€β”€ .env                  # Environment variables (create from .env.example)
β”œβ”€β”€ middleware/
β”‚   └── error.js         # Error handling middleware
β”œβ”€β”€ routes/
β”‚   └── index.js         # API routes
└── public/              # Client-side files
   β”œβ”€β”€ index.html       # Main HTML file
   β”œβ”€β”€ style.css        # Styles with modern design
   └── main.js          # Client-side JavaScript

πŸ”Œ Adapting for Other APIs

This proxy server is designed to be flexible. To use it with different APIs:

  1. Update the .env file with your API's base URL and key format
  2. Modify the routes in routes/index.js if needed
  3. Adjust the client-side code in public/main.js to handle your API's response format

Example: GitHub API

API_BASE_URL=https://api.github.com/users
API_KEY_NAME=Authorization
API_KEY_VALUE=token your_github_token

πŸ›‘οΈ Security Features

  • βœ… API keys stored securely in environment variables
  • βœ… Rate limiting to prevent abuse
  • βœ… CORS configuration
  • βœ… Error handling middleware
  • βœ… Input validation

πŸš€ Deployment

This server can be deployed to various platforms:

  • Heroku: git push heroku main
  • Vercel: Deploy with Vercel CLI
  • Railway: Connect GitHub repository
  • DigitalOcean: Deploy on App Platform

Remember to set environment variables in your deployment platform!

πŸ“ License

This project is open source and available under the MIT License.

🀝 Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the issues page.

πŸ“§ Contact

For questions or support, please open an issue in the GitHub repository.


Made with ❀️ using Node.js and Express

About

Utilizing Node.js and Express to create a server for enhanced security with hidden API keys, rate limiting, and efficient caching to optimize API performance.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published