With its large collection of (free) professional imagery and easy-to-use developer API, Unsplash is a great tool for generating contextual placeholder images for your application.
There are numerous sites that serve placeholder images. These are great when working in development and wanting to see how real images will behave in your application.
The challenge is that you almost always need to know the context of an image to use one of these services if it's going to look like it fits. Otherwise, you're just using random images that may not provide the proper feel or context for where they are being used.
For example, say you're building a pet adoption application. Photos of Nicolas Cage wouldn't feel quite right. Photos of kittens would, but you might want this to feel more real and balanced, including other animals. And most of these services are not built to be dynamic.
Fortunately, Unsplash has an API that includes searching for images that can help you grab more contextual images.
Here's a simplified process for including dynamic placeholder images in your application.
To use the Unsplash API, you need to create a developer account and register an application. The docs are well-written and make this an easy process.
In this example, we'll use three dependencies:
Install the necessary dependencies:
npm install --save-dev unsplash-js node-fetch @faker-js/faker
Here's some basic code that will fetch images from Unsplash and write the response to a file called photos.json
in your project. Note that it is using a UNSPLASH_ACCESS_KEY
environment variable that contains the API key given to your Unsplash developer application.
import fetch from "node-fetch";
import fs from "fs";
import path from "path";
import { createApi } from "unsplash-js";
import { faker } from "@faker-js/faker";
global.fetch = fetch;
// Initiate Unsplash API
const unsplash = createApi({ accessKey: process.env.UNSPLASH_ACCESS_KEY });
// Search photos for a random animal (from Faker library)
const photos = await unsplash.search.getPhotos({ query: faker.animal.type() });
// Write response to a photos.json file
const filePath = path.join(process.cwd(), "photos.json");
fs.writeFileSync(filePath, JSON.stringify(photos, null, 2));
After running this code, inspect the photos.json
file to see the shape of the response, assuming there were images that matched the random image.
Unsplash uses Imgix to serve its images, which means that you can manipulate them using their rendering API.
You may search insect
and be returned this image:
But you can resize and manipulate this image as needed. For example, you can crop it to a square.
Once you implement the code to fit your application, here are some additional suggestions for building on this code:
There are a couple limitations and restrictions with the code that you should be aware of when implementing this solution:
ixid
parameter that is included with every URL. This is how Unsplash knows you are using the image and it is part of the guidelines. If you manipulate the URL parameters, be sure to keep ixid
intact.