Learn how to integrate v0 with Pixabay API to power your site with quality images. Follow our step-by-step guide for seamless and efficient setup.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Create a new file named pixabayService.ts in your project. This file will contain the TypeScript code to fetch images from the Pixabay API. Paste the following code into that file:
export interface PixabayResponse {
total: number;
totalHits: number;
hits: Array<{
id: number;
pageURL: string;
type: string;
tags: string;
previewURL: string;
previewWidth: number;
previewHeight: number;
webformatURL: string;
webformatWidth: number;
webformatHeight: number;
largeImageURL: string;
imageWidth: number;
imageHeight: number;
imageSize: number;
views: number;
downloads: number;
collections: number;
likes: number;
comments: number;
user_id: number;
user: string;
userImageURL: string;
}>;
}
export async function fetchImages(query: string, apiKey: string): Promise {
const url = https://pixabay.com/api/?key=${apiKey}&q=${encodeURIComponent(query)}&image_type=photo;
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data: PixabayResponse = await response.json();
return data;
}
Decide where you want to use the Pixabay integration. This could be your main application file (for example, app.ts) or another part of your code. In that file, import and use the fetchImages function as shown below:
import { fetchImages } from './pixabayService';
// Replace 'YOURPIXABAYAPI_KEY' with your actual Pixabay API key.
const apiKey = 'YOURPIXABAYAPI_KEY';
async function searchImages() {
try {
const result = await fetchImages('nature', apiKey);
console.log('Total Images Found:', result.totalHits);
console.log(result.hits);
} catch (error) {
console.error('Error fetching images:', error);
}
}
searchImages();
Since your v0 project does not have access to a terminal for dependency installation, you need to manually ensure that your project supports the fetch API. If your project is meant for a browser environment, the built-in fetch is available. However, if you are targeting a Node.js environment that lacks fetch, add this import at the very top of your pixabayService.ts file:
import fetch from 'node-fetch';
Also, create or update your package.json file to include node-fetch as a dependency. Since you cannot run a terminal command, add the following content manually to your package.json file:
{
"dependencies": {
"node-fetch": "^2.6.1"
}
}
After adding the above code snippets, compile your TypeScript code (if necessary) using your project's build process. Once compiled, run your project. Open the browser console (if running in a browser) or check your terminal for logs. The output should show the total number of images found and details of the image hits fetched from Pixabay.
app.ts (or your main file) is correctly set.fetchImages function to adjust query parameters if you need a different search term or to include more options from the Pixabay API.fetch function. If you are using Node.js, verify that the node-fetch dependency is properly referenced.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.