Files
classroomio/apps/dashboard/src/routes/api/unsplash/+server.ts
T
2024-09-20 11:16:19 +01:00

37 lines
943 B
TypeScript

import { json } from '@sveltejs/kit';
import { env } from '$env/dynamic/private';
const UNSPLASH_API_URL = `https://api.unsplash.com/search/photos?page=2&per_page=15&auto=format&fit=crop&w=2970&q=80&client_id=${env.UNSPLASH_API_KEY}`;
// API to fetch photos from Unsplash
export async function POST({ request }) {
const body = await request.json();
const { searchQuery } = body;
const query = searchQuery || 'rocks';
try {
const response = await fetch(`${UNSPLASH_API_URL}&query=${encodeURIComponent(query)}`, {
method: 'GET'
});
if (!response.ok) {
throw new Error('Unable to fetch photos from Unsplash API');
}
const data = await response.json();
return json({
success: true,
photos: data.results
});
} catch (error) {
console.error(error);
return json(
{ success: false, message: 'Error fetching photos from Unsplash' },
{ status: 500 }
);
}
}