Fetch images from Unsplash and write the fetched images data to a JSON file.
I wanted to use a random list of specified images on a project. There are various approaches to achieve this result:
- Manually download the images and save them to disk
- Create a JSON file with the properties of the images
I decided to implement a simple logic where I fetch images from Unsplash and then write the image data to a JSON file. From the JSON file, I could easily consume the data. Using this process, I saved storage space since I wouldn't have to download the images.
-
Register as a developer on Unsplash from this link: https://unsplash.com/developers.
-
Clone this repository:
git clone https://github.com/khwilo/unsplash-images-json.git
-
Create a
.env
file and add your API access key. View the format in the.env-sample
that is provided in the project root directory. -
Run
yarn
ornpm install
to install the project dependencies. -
Run
yarn start
ornpm start
to run the application.
This project has a function called fetchListOfVariousPhotos
that takes two arguments: queries and transform. queries is a list of search terms. transform is a boolean value. Specifying the transform property to the equal the value true; transforms the format of the written JSON file to the following:
Property | Type |
---|---|
id | string |
description | string |
alt_description | string |
imgThumb | string |
img | string |
link | string |
userId | string |
userName | string |
userLink | string |
tags | [ ] string |
The saved JSON file can be found in the project data directory.
- Make this project to be a CLI app
Author: Khwilo Kabaka