- Create Lambda function with secret environment variable - Add YouTube API function to backend configuration - Create Next.js API route to handle YouTube requests - Update gallery page to use API route - This follows the correct Amplify Gen 2 pattern for secrets 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
46 lines
No EOL
1.1 KiB
TypeScript
46 lines
No EOL
1.1 KiB
TypeScript
import type { APIGatewayProxyHandler } from 'aws-lambda';
|
|
|
|
export const handler: APIGatewayProxyHandler = async (event) => {
|
|
const apiKey = process.env.YOUTUBE_API_KEY;
|
|
|
|
if (!apiKey) {
|
|
return {
|
|
statusCode: 500,
|
|
body: JSON.stringify({ error: 'YouTube API key not configured' }),
|
|
};
|
|
}
|
|
|
|
const videoIds = JSON.parse(event.body || '{}').videoIds || [];
|
|
|
|
if (!videoIds.length) {
|
|
return {
|
|
statusCode: 400,
|
|
body: JSON.stringify({ error: 'Video IDs are required' }),
|
|
};
|
|
}
|
|
|
|
try {
|
|
const url = `https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${videoIds.join(',')}&key=${apiKey}`;
|
|
const response = await fetch(url);
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`YouTube API error: ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
statusCode: 500,
|
|
body: JSON.stringify({ error: 'Failed to fetch YouTube data' }),
|
|
};
|
|
}
|
|
}; |