Back

Unsplash API Random Image

Unsplash API Random Image

Unsplash is a great resource to get free, high quality images. They have an API that allows you to get random images based on a search query. This is a great way to get a random image for a blog post or article.

However, instead of using their API you can directly assemble URLs as queries.

The header image for this article uses the following URL: https://source.unsplash.com/random/?city,night&orientation=landscape

You can also specify size by adding it before the query string. e.g. https://source.unsplash.com/1920x1080/random/?city,night&orientation=landscape

You can find all the query parameters in the [Unsplash API documentation](https://unsplash.com/documentation#get-a-random-photo. Then it is just a matter of converting a parameter to a query string.

e.g. orientation | Filter by photo orientation. (Valid values: landscape, portrait, squarish) becomes orientation=landscape

If its the first parameter in the query its preceded by ? otherwise its preceded by &.

The city,night part of the URL above is implicitly query=city,night which is why it is preceded by ?.