
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
secure-server-fetch
Advanced tools
A secure, server-side HTTP client with built-in API key validation, rate limiting, and security features
A secure, server-side HTTP client with built-in API key validation, rate limiting, and security features.
npm install secure-server-fetch
# or
yarn add secure-server-fetch
Create a .env file in your project root:
UPSTASH_REDIS_REST_URL=your_redis_url
UPSTASH_REDIS_REST_TOKEN=your_redis_token
Both variables are required for rate limiting functionality. The Redis URL must be a valid HTTPS URL.
import { secureServerFetch } from 'secure-server-fetch';
async function fetchData() {
try {
const data = await secureServerFetch('https://api.example.com/data', {
apiKey: 'your-api-key',
timeout: 5000, // 5 seconds
});
return data;
} catch (error) {
console.error('Fetch failed:', error);
}
}
import { requireApiKey } from 'secure-server-fetch';
// In your API route handler
export async function handler(request: Request) {
const apiKeyCheck = requireApiKey(request, 'your-expected-api-key');
if (apiKeyCheck) {
return apiKeyCheck; // Returns 401 response if validation fails
}
// Continue with your API logic
}
The API key validation enforces the following requirements:
All error responses are returned in JSON format with appropriate HTTP status codes:
{
"error": "API key is missing",
"message": "Please provide 'x-api-key' header with a valid API key",
"requirements": {
"format": "Must be at least 32 characters long",
"characters": "Can only contain letters, numbers, underscores, and hyphens",
"complexity": "Must contain at least one uppercase letter, one lowercase letter, and one number"
}
}
{
"error": "Empty API key",
"message": "API key cannot be empty",
"requirements": {
"format": "Must be at least 32 characters long",
"characters": "Can only contain letters, numbers, underscores, and hyphens",
"complexity": "Must contain at least one uppercase letter, one lowercase letter, and one number"
}
}
{
"error": "Invalid API key format",
"message": "[Specific validation error message]",
"requirements": {
"format": "Must be at least 32 characters long",
"characters": "Can only contain letters, numbers, underscores, and hyphens",
"complexity": "Must contain at least one uppercase letter, one lowercase letter, and one number"
}
}
{
"error": "Invalid API key",
"message": "The provided API key is not valid"
}
import { rateLimit } from 'secure-server-fetch';
async function handleRequest(request: Request) {
try {
const rateLimitResult = await rateLimit(request, 'unique-identifier', {
maxRequests: 100,
timeframeMs: 60000, // 1 minute
});
// Rate limit headers will be automatically included in rateLimitResult.headers
// Your API logic here
} catch (error) {
if (error.name === 'RateLimitError') {
return new Response('Rate limit exceeded', { status: 429 });
}
}
}
Makes a secure server-side HTTP request.
apiKey?: string - API key for authenticationrequireHttps?: boolean - Enforce HTTPS (default: true)timeout?: number - Request timeout in ms (default: 30000)...RequestInit - All standard fetch optionsValidates API key from request headers.
request: Request - Incoming request objectexpectedKey: string - The API key to validate againstImplements rate limiting for requests.
maxRequests: number - Maximum requests allowedtimeframeMs: number - Time window in millisecondsprefix?: string - Redis key prefixAPI Keys
Rate Limiting
HTTPS
Error Handling
The library provides specific error classes:
ServerSideError: For client-side execution attemptsNetworkError: For network and HTTP errorsValidationError: For input validation failuresRateLimitError: For rate limit violationsContributions are welcome! Please read our contributing guidelines and submit pull requests.
MIT
FAQs
A secure, server-side HTTP client with built-in API key validation, rate limiting, and security features
We found that secure-server-fetch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.