this is aaronland

things I have written about elsewhere #20250114

The SFO Museum Application Programming Interface (API)

Negative: San Francisco International Airport (SFO), aerial. Negative. Transfer from San Francisco International Airport, SFO Museum Collection. 2011.032.0774

This was originally published on the SFO Museum Mills Field weblog, in January 2025.

Today we are announcing the availability of the SFO Museum Application Programming Interface (API). The API allows developers to access SFO Museum-related data programmatically over the internet (HTTP). Use of the API requires that you create a SFO Museum account and agree to the Museum's terms of service and community guidelines. The root URL for all API requests, configuration details and documentation is:

API Methods

The SFO Museum API exposes a number of API methods grouped by theme or topic. As of this writing they are:

Every API method has a stable, permanent URL with documentation for its input parameters and other details. For example the documentation page for the sfomuseum.you.shoebox.listItems page looks like this:

All API calls are sent to https://api.sfomuseum.org/rest as either an HTTP GET or POST request, with two or more query parameters. For example, to list all the items added to your SFO Museum shoebox in 2024:

$> curl -X GET \
   	-F method=sfomuseum.you.shoebox.listItems \
	-F access-token={SFOMUSEUM_API_ACCESS_TOKEN} \
	-F min_date=1704067200 \
	-F max_date=1735689599 \
	https://api.sfomuseum.org/rest

The ?method= and ?access_token= parameters are required for all API calls.

Access Tokens

Commemorative token: Golden Gate International Exposition, San Francisco. Metal. Gift of Elsa Cameron, SFO Museum Collection. 1997.54.05.04

Access to the API is governed through the use of "access tokens", specifically "grant tokens" as defined in section 4.1 of the OAuth2 specification. These tokens allow third-party developers to create applications which can act on your behalf without the need for you to share your SFO Museum account credentials. Instead, a third-party application will ask you to allow permission to act on your behalf and, once granted, will be issued a new "grant token" which enables that access.

That may be fine if you are a developer but what if you just want an access token to your for your own personal use? To address this need we've created a simple "Create a new access token for yourself" webpage:

This webpage allows you to create a personal access token by entering three pieces of information: A name or label for the access token, the level of permissions you want the access token to have and how long you want that token to last.

Tokens may have the following permissions:

Tokens may also be assigned an optional "time to live". These are:

If a time to live is defined then an expiry date will be assigned, starting from the moment the access token was created, and will be checked on each API request. The default is to allow access tokens to work until they are manually deleted (from the api.sfomuseum.org website).

Remember: Access tokens allow third parties and automated processes to act on your behalf and should be treated as sensitive data.

Keychain: Flying Tiger Line. Metal, plastic, ink. Gift of Thomas G. Dragges in memory of Robert May, SFO Museum Collection. 2001.109.014

API Explorer

You can also try (almost) all of the API methods from the api.sfomuseum.org website itself. Next to the description for each method, on its individual documentation page, there is a "Try this method in the API Explorer" link. That link will take you to a new page with a form displaying all the available parameters for that API method:

You can enter any specific values you want to test the API with as well as indicating the types of permissions you want the API call to be performed with.

Executing the API request will display both the raw HTTP request (as a cURL command) and the results returned by the API. There is no formal definition (yet) for API responses so the API Explorer is a good way of investigating how to use the API.

API Clients

Exhibition poster: San Francisco Airports Commission, Toys That Travel. Paper, ink. Transfer from San Francisco International Airport, SFO Museum Collection. 2017.079.011

We have released library clients for the SFO Museum API for both the Python and Go programming languages. The design of the API, and by extension our client libraries, is minimalist in nature. There is a single endpoint which takes a method parameter, two or more additional parameters and returns JSON responses. It is left to application using the API and libraries to parse that JSON and extract data as deemed necessary.

If you have created or know of any other client libraries for use with the SFO Museum API let us know and we'll add them to the list of libraries on the SFO Museum API website.

Python

https://github.com/sfomuseum/py-sfomuseum-api

from sfomuseum.api.client import OAuth2Client
import json

access_token = "SFOMUSEUM_API_ACCESS_TOKEN"
kwargs = { "method": "api.spec.methods" }

cl = OAuth2Client(access_token)
rsp = cl.execute_method("GET", kwargs)

data = json.load(rsp)
print(json.dumps(data))

Go

https://github.com/sfomuseum/go-sfomuseum-api

package main

import (
	"context"
	"io"
	"net/url"
	"os"

	"github.com/sfomuseum/go-sfomuseum-api/client"
)

func main() {

	ctx := context.Background()

	client_uri := "oauth2://?access_token={TOKEN}"
	cl, _ := client.NewClient(ctx, client_uri)

	args := &url.Values{}
	args.Set("method", "api.spec.methods")

	fh, _ := cl.ExecuteMethod(ctx, "GET", args)
	defer fh.Close()
	
	io.Copy(os.Stdout, fh)
}

"picturebook"

To demonstrate the use of the SFO Museum API and the go-sfomuseum-api package we have also released the sfomuseum/go-picturebook-sfomuseum package. This package includes a command-line tool called picturebook which enables the creation of "picturebooks" – PDF files created from a set of images using the SFO Museum API. Currently, it is only capable of producing picturebooks derived from collection objects and Instagram posts save to your SFO Museum shoebox but in the future it will be able to create picturebooks from arbitrary API calls.

To use picturebook first download the source code GitHub and compile it. To create a PDF file of all the items in your SFO Museum shoebox run the tool passing in your SFO Museum API access token and any other PDF-specific arguments. For example:

$> git clone https://github.com/sfomuseum/go-picturebook-sfomuseum.git
$> cd go-picturebook-sfomuseum
$> make cli

$> ./bin/picturebook \
	-width 6 \
	-height 9 \
	-access-token {SFOMUSEUM_API_ACCESS_TOKEN}

Which would create a document that looks like this:

Each object image will include a caption like this:

postcard: Lufthansa, Ilyushin Il-14, Berlin Airport
1940s
Gift of Thomas G. Dragges
Collection of SFO Museum
https://collection.sfomuseum.org/objects/1762696177/
2015.166.0976

Instagram posts will look like this:

And their captions like this:

"First flown in 1957, the Boeing 707 was the first successful jet airliner to provide regular, sustained commercial passenger service."
This was posted to the SFO Museum Instagram account on October 04, 2024
https://millsfield.sfomuseum.org/instagram/1947424849

You can also download a copy of the picturebook PDF described here (21MB) to see what the finished product looks like. It is not currently possible to export individual flights saved to a shoebox but the hope is that this will be supported soon.

Photograph: Pan American World Airways, Boeing 707. Photograph. Gift of M.D. Klaas, SFO Museum Collection. 2018.112.0767

The SFO Museum API has actually been around for a while now. It's what powers the object counts as you add and remove filters on the advanced search page on the Aviation Collection website and is how items are added to and removed from your shoebox from both the Collection and Mills Field websites. The various SFO Museum websites are API consumers just like any other application. We are pleased to be able to (finally) open up access to the API to you and we look forward to seeing what you create with it.