-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (42 loc) · 1.33 KB
/
index.js
File metadata and controls
49 lines (42 loc) · 1.33 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
export const handler = async (event) => {
let symbol;
try {
// Parse the body if it's a string (Function URL format)
if (event.body && typeof event.body === 'string') {
const parsedBody = JSON.parse(event.body);
symbol = parsedBody.symbol;
} else {
// Direct invocation format
symbol = event.symbol;
}
} catch (error) {
return {
statusCode: 400,
body: JSON.stringify('Invalid JSON in request body.')
};
}
if (!symbol) {
return {
statusCode: 400,
body: JSON.stringify('Missing parameter: symbol is required.')
};
}
const apiKey = 'your_alpha_vantage_api_key';
const url = `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${symbol}&apikey=${apiKey}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return {
statusCode: 200,
body: JSON.stringify(data)
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify('Error fetching data from Alpha Vantage API.')
};
}
};