Repository navigation
Expand file tree
/
Copy pathfred-data.js
More file actions
51 lines (43 loc) · 1.49 KB
/
Copy pathfred-data.js
File metadata and controls
51 lines (43 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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' },
});
}
}