Learn how to integrate Lovable with the Pixabay API in simple steps. Enhance your website with high-quality images while streamlining your content creation.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
pixabayService.ts
.
export interface PixabayImage {
id: number;
pageURL: string;
previewURL: string;
largeImageURL: string;
}
export interface PixabayResponse {
total: number;
totalHits: number;
hits: PixabayImage[];
}
export class PixabayService {
// Replace 'YOURPIXABAYAPI_KEY' with your actual Pixabay API key
private apiKey: string = 'YOURPIXABAYAPI_KEY';
private baseUrl: string = 'https://pixabay.com/api/';
public async searchImages(query: string): Promise<PixabayImage[]> {
const url = ${this.baseUrl}?key=${this.apiKey}&q=${encodeURIComponent(query)}&image_type=photo;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data: PixabayResponse = await response.json();
return data.hits;
} catch (error) {
console.error('Error fetching images:', error);
return [];
}
}
}
app.ts
) in your Lovable project.PixabayService
class from the file you just created.
import { PixabayService } from './pixabayService';
const pixabay = new PixabayService();
function displayImages(images: any[]): void {
const container = document.getElementById('imageContainer');
if (container) {
// Clear previous content
container.innerHTML = '';
images.forEach((img) => {
const imgElement = document.createElement('img');
imgElement.src = img.previewURL;
imgElement.alt = 'Pixabay Image';
// Optional: Set styling properties for the image
imgElement.style.margin = '5px';
container.appendChild(imgElement);
});
}
}
// Example: Search for images with the query "nature" on page load
window.addEventListener('DOMContentLoaded', async () => {
const images = await pixabay.searchImages('nature');
displayImages(images);
});
index.html
) in your Lovable project.imageContainer
where the images will be displayed.
Lovable Pixabay Integration
<script src="https://cdn.example.com/library.min.js"></script>
YOURPIXABAYAPI_KEY
in pixabayService.ts
with your actual Pixabay API key.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.