DEV Community

Arnav Motwani
Arnav Motwani

Posted on

The most basic Unsplash API example (Probably.)

I want to start of this post by saying that I am not the most advanced python programmer around by any means and I am just learning about API's myself. There's one API I was actually been very curious about and I wanted to build projects on it; the Unsplash API. For those who don't know Unsplash is photo sharing community with free to use images (of course crediting the photographer in anything public), as a photographer and budding app builder it was love at first site. Not only was the photography astoundingly good, the app and website is minimal and very well designed. Needless to say it was built by some amazing developers but my greatest discovery was their API. While it is pretty simple to use, as a beginner it was daunting but through some trial I made it simple. The problem was that I found nothing on the internet that was easy to understand and learn from so I taught myself little by little until I made something that worked.

TLDR: I like the Unsplash API, here's my example.

import requests
from PIL import Image
from io import BytesIO

def linkFetch():
    url = ""

    response = requests.get(url)
    data = response.json()["urls"]["raw"]
    return data

img_url = linkFetch()
response = requests.get(img_url)
img =
Enter fullscreen mode Exit fullscreen mode

Note: My actual API key has been replaced by MyAccessKey

Okay this might look complicated (slightly) but let me break it down.
You are importing three packages: requests, Image (from PIL) and io (from BytesIO). The requests package actually gets the data from the API while the other two packages are used to open the image.

Look at the linkFetch() method, this method uses the aforementioned requests package. First I stated my url, this is where I would add my unique access key as well as the parameters for the image I want returned. The Url can be split into three parts:, /photos/random and /?client_id=MyAccessKey

The first part tell the computer which API to access, the second part shows the parameters (in my case, I wanted a random photo) and finally the third is the authentication part which contains your access key. You can get your access key on the Unsplash API page: which also has the documentation that includes with the full list of parameters.

Look at the next two lines:

response = requests.get(url)
data = response.json()["urls"]["raw"]

These lines are use to get data from the API and then store in an easy to use dictionary. I used the requests package to get data from the url. Then I used the .json tag to convert it to a dictionary, from said dictionary within the urls object I took the raw link for the image and stored that link in the data variable which is what the method returns when called.

I stored the returned link into img_url for clarity and then used another get request to get the image. This request is different though as this time it is receiving data from a website rather than an API. Then I used BytesIO to make the data for the image useable and finally opened the image using the image library.

I hope this was clear as the was my first post and I'm happy to respond to any question you might have.


Top comments (0)