Learn how to integrate Lovable with the Shutterstock API in our easy guide. Unlock seamless image access and boost your project in just a few simple steps.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
In your Lovable project, create a new TypeScript file called shutterstockAPI.ts
in the main source folder. This file will contain all the functions needed to communicate with the Shutterstock API.
// Replace 'YOURSHUTTERSTOCKACCESS_TOKEN' with your actual Shutterstock API access token.
export const SHUTTERSTOCKACCESSTOKEN = 'YOURSHUTTERSTOCKACCESS_TOKEN';
// Base URL for the Shutterstock API.
export const SHUTTERSTOCKBASEURL = 'https://api.shutterstock.com/v2';
/**
- Function to search images on Shutterstock using a query string.
- @param query - The search term for images.
- @returns A promise resolving with the JSON result from the API.
*/
export async function searchImages(query: string): Promise {
const url = ${SHUTTERSTOCK_BASE_URL}/images/search?query=${encodeURIComponent(query)};
const response = await fetch(url, {
headers: {
'Authorization': Bearer ${SHUTTERSTOCK_ACCESS_TOKEN},
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(Error fetching images: ${response.statusText});
}
return await response.json();
}
Now, open your main project file where you want to use the Shutterstock API (for example, main.ts
) and import the function from shutterstockAPI.ts
. Here’s how to do it:
import { searchImages } from './shutterstockAPI';
// Example function to handle image search triggered from your application.
async function handleImageSearch(query: string) {
try {
const results = await searchImages(query);
console.log('Search Results:', results);
// Process the results as needed in your application.
} catch (err) {
console.error('Error during image search:', err);
}
}
// Example: Trigger a test search when the app starts.
handleImageSearch('nature');
Lovable does not have a terminal for installing dependencies. Since modern browsers and many runtimes already support the fetch
API, no external library is needed. If your project requires a polyfill for fetch
(for example, if running on an older Node.js version), include the polyfill directly in your project by adding its script tag in your HTML file or by copying the polyfill code into your project as a new file.
For example, if you needed to polyfill fetch
using a script, add the following to your main HTML file:
In the file shutterstockAPI.ts
, ensure that you replace 'YOURSHUTTERSTOCKACCESS_TOKEN'
with your actual Shutterstock API access token. This token is used to authenticate your API requests.
// Example: Replace with your access token.
export const SHUTTERSTOCKACCESSTOKEN = 'abc123yourRealAccessToken';
Once the above changes are in place, your Lovable project will call the Shutterstock API when handleImageSearch
is invoked. Open your project in the browser, and check the console output for the search results or any error messages. Ensure your API token is valid and your network connection is active.
After successfully testing, you can expand the integration by:
• Implementing additional functions for other Shutterstock endpoints (for example, image details or downloading images).
• Creating user interface elements to allow dynamic search queries from users.
• Handling errors and loading states for a smoother user experience.
By following these steps, you have integrated the Shutterstock API into your Lovable project using TypeScript.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.