Feb 22, 2020 08:15 AM
This base uses the Unsplash API to search for images and save them to your base for review.
The base is published on the Airtable Universe here.
To use this base, copy it from the Universe into your workspace. You will need an Airtable Pro account and access to the scripting block to make it work.
To get going, go to the Unsplash Developer pages, create an account, then create an Unsplash application. This will create an access key and a secret key for the app (we only need the access key).
Open the script block in Airtable, click on “Edit” and replace the access key default string with your access key:
Close the script editor and run the script. You will be prompted to enter a search term:
Enter your term and click on “Next”. The script will pull 10 images from Unsplash using their API based on your search term. These will be stored in the “Photos” table:
For a nicer look, switch to the gallery view:
The script saves the image, the image link, 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, but any questions, please add a comment below.
Happy photo hunting!
JB
Feb 22, 2020 08:20 AM
This is the script:
// 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}]
});
})
Feb 23, 2020 12:31 AM
This is pretty nifty.
Makes me think what can be done with automated pdf creation.
ie:
this would sort of solve the feature request where folks would like to “save the PDF from page designer” to the record
Create PDF easily with our developers API/SDK for Ruby Java .NET C# PHP Shell Delphi.
Feb 24, 2020 10:58 PM
I’ve had to unpublish the base from the Airtable Universe for now. There were some breaking changes to the Script Block (see here). Not a big deal for my example above, but now when you access the base from the Universe the script is lost and the default script shown.
In the meantime, here’s a link that users can access the base with:
JB
Feb 26, 2020 04:23 PM
@JonathanBowen we just added a setting for controlling when scripts should be shared:
Do you mind turning that on in your shared base & re-publishing to Universe? Thank you!
Feb 27, 2020 11:19 AM
@Kasra - thanks, now working here:
Oct 01, 2020 03:45 PM
Thanks a lot. This is pretty useful. My first script and it was a great experience understanding how it works.
Nov 09, 2021 12:23 PM
Hey @JonathanBowen ,
Seems like all the links to your base are broken. Would you mind re-sharing it? Would help out a ton :pray:
Nov 11, 2021 12:01 PM
Hi @Marco1 - I don’t have this base in the universe any more, but if you create a base and add a table (named Photos) with the following fields:
Then add the script further up in this post. It should all work OK.