BuiltOnAir

Saving images from Unsplash

Created by: Creator: Jonathan Bowen

External API

This script was created and added into an Airtable Universe base that you can find here.

The Universe base uses the Unsplash API to search and save images for you into your base.

This script lets you save images, image links, the photographer’s name and the search term you entered. The script is marked up with comments to explain what it is doing at each step.

To see the written tutorial and steps to get set up into your own bases look here.

Script Code


							// Add your Unsplash API access key here
let access_key = "YOUR_API_ACCESS_KEY";

// set the unsplash search url
let unsplash_url = 'https://api.unsplash.com/search/photos?query=';

// get the search term from the user
let query = await input.textAsync('Enter a photo search term');

// make the full unsplash url to be queried
let url = unsplash_url + query.toLowerCase();

// set the headers required to make the API call to Unsplash
let headers = {
    "Accept-Version": "v1",
    "Authorization": "Client-ID " + access_key
}

// set the table that stores the photos
let table = base.getTable("Photos");

// make the API call
let response = await fetch(url, {
    method: "GET",
    headers: headers
})

// get the API response
let data = await response.json();

// for each result in the response, create a record in Airtable
// saving the photographer's name, link to the photo on Unsplash 
// and save the photo into Airtable
// Other fields are returned - uncomment the console.log below to see options
data.results.forEach(async photo => {
    // console.log(photo);
    let newRecord = await table.createRecordAsync({
        "Search Term": query,
        "Photographer": photo.user.name,
        "Link": photo.links.html,
        "Photo": [{"url": photo.urls.regular}]
    });    
})