Files
worldmonitor/api/fred-data.js
Elie Habib a46d98dae1 Move FRED API key to server-side for security
- Updated api/fred-data.js serverless function to use FRED API with
  server-side API key (via FRED_API_KEY env var)
- Removed hardcoded API key from frontend code
- Frontend now calls /api/fred-data without exposing the API key
- Updated vite proxy config for local dev

IMPORTANT: Add FRED_API_KEY to Vercel environment variables!

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-11 00:32:03 +04:00

52 lines
1.5 KiB
JavaScript

export const config = { runtime: 'edge' };
export default async function handler(req) {
const url = new URL(req.url);
const seriesId = url.searchParams.get('series_id');
const observationStart = url.searchParams.get('observation_start');
const observationEnd = url.searchParams.get('observation_end');
if (!seriesId) {
return new Response('Missing series_id parameter', { status: 400 });
}
const apiKey = process.env.FRED_API_KEY;
if (!apiKey) {
return new Response('FRED_API_KEY not configured', { status: 500 });
}
try {
const params = new URLSearchParams({
series_id: seriesId,
api_key: apiKey,
file_type: 'json',
sort_order: 'desc',
limit: '10',
});
if (observationStart) params.set('observation_start', observationStart);
if (observationEnd) params.set('observation_end', observationEnd);
const fredUrl = `https://api.stlouisfed.org/fred/series/observations?${params}`;
const response = await fetch(fredUrl, {
headers: { 'Accept': 'application/json' },
});
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'public, max-age=3600',
},
});
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}