Comment on page
🖥

API - for developers

Hack the planet
Did You Know? Developers are super cool
Hello hackers! This is an early/WIP beta, but we’re excited to see what people are hacking together with this ¯¯\(ツ)/¯¯
For questions please join our Discord or get in touch. If you'e made something cool, let us know! We'd love to promote it.
Some glif-powered things:

Simple API - “just run a glif”

Since working with the more advanced graph-run API endpoint (below) requires authentication headers, HTTP-streamed JSON, and doesn’t have CORS headers out of the box, we have built a little app that wraps all that together into a simple RESTful API, using a shared API key, tied to our internal @Glifbot user:
During the limited Glif open beta, this API does not require authentication and has a relatively low rate limit. This will change in the future. If you're building with our API and don't want to have any disruptions, please join our Discord or get in touch and we put you on a special list for dedicated API keys.

Example

Here’s an example request using curl:
curl -X POST -d '{"input": ["cute friendly oval shaped bot friend"]}' https://simple-api.glif.app/clgh1vxtu0011mo081dplq3xs
Here’s the expected output of that command:
{
"input": "cute friendly oval shaped bot friend",
"output":"<https://res.cloudinary.com/dzkwltgyd/image/upload/v1686242317/glif-run-outputs/fhbvbp9bwf0pkmm4woj2.png>"
}
If there’s an error it’ll be in an error field and reflected in the HTTP status code

Parameters

There are multiple ways to pass in input values to a glif
The simplest is an array in a field called inputs, like this:
{
"inputs": ["i am the walrus"]
}
You can also POST directly to simple-api.glif.app with no id in the path, and specify the id inside the payload, like:
in which case you only need to send the input param, like:
{
"id": "clgh1vxtu0011mo081dplq3xs",
"inputs": ["I am the walrus"]
}
The inputs param is fairly flexible and tries to accomodate a lot of different ways of providing data to the glif.
If you do not provide enough inputs, the glif's default values will be automatically substituted. If you'd prefer that to fail, specify ?strict=1, e.g. https://simple-api.glif.app?strict=1
For a single-input glif, you can just specify a single string:
{
"inputs": "a happy horse"
}
For a multi-input glif, you can use a positional array like:
{
"inputs": ["a happy horse", "living on a farm", "in France"]
}
or alternately, as a full object with the actual names of each input field:
{
"inputs": {
subject: "a happy horse",
actionName: "living on a farm",
location: "in France"
}
}
And lastly, if you are specifying input names, you can leave off the inputs key and just pass the object to your glif, like this:
{
subject: "a happy horse",
actionName: "living on a farm",
location: "in France"
}

Looking up the names of a glif's inputs

To specify named parameters, you need to use the internal block name. This has no spaces and usually is something like "text1", "multipick2", but each glif creator can customize it.
On the glif.app website you can look at the block names in the "View source" page of a glif, like this one:
Or you can look at the nodes field of a glif via our API:

Full API

Running a glif - with HTTP streaming

The full API currently requires using named parameters and does not support the generic input field, like the Simple API
The response is a stream of JSON objects, rather than a single JSON object. You can split them on newlines "\n", grab last one, and grab the output field for the final piece of text or image URL.
The streaming response allows you to provide user updates as each block executes, but 90% of the time we just get the final entry.
To use the API:
  1. 1.
    set your API key -- contact us and we will send you one. Nice UI coming soon:
export GLIF_API_TOKEN="foobar12345glifrocks"
  1. 2.
    Then call the API using my favorite http client, httpie:
echo '{"id": "clmdnx8fa0000lf0fbovo8lv1", "input1": "cute friendly oval shaped bot friend"}' | http --stream -A bearer -a $GLIF_API_TOKEN POST "https://glif.app/api/run-glif"
To process the json object stream and grab the final output field, I'd do something like this on the commandline:
res=$(echo '{"id": "clmdnx8fa0000lf0fbovo8lv1", "input1": "cute friendly oval shaped bot friend"}' | http --stream -A bearer -a $GLIF_API_TOKEN POST "<https://alpha.glif.xyz/api/run-glif>")
echo $res | jq -s 'last' jq '.output'
  • curl equivalent to httpie would be something like curl -x POST -H "Bearer ..." but I think you see the basics
  • needs id and input object (if applicable) as POST body
currentlyinput object must be an object with keys matching the glifs inputs exactly. To see the glif's input names, visit e.g.:
example of fetching field names dynamically using curl + jq:
curl -s https://glif.app/api/glifs?id=clmdnx8fa0000lf0fbovo8lv1 | jq -r '.[] .data .nodes[] .name'
here's the output:
input1
image1
meaning this glif requires two inputs, input1 and image1. We plan to improve this in the near future. In the meantime, try the Simple API above

Other API endpoints

These endpoints are alpha-quality only and subject to change
Eg for fetching info about a specific glif, user, or a specific glifRun.
Paginate using ?page= when applicable
Individual glif
  • fetch a specific glif: GET https://glif.app/api/glifs?id=clgh1vxtu0011mo081dplq3xs
  • includes data on its most recent runs
Lists of glifs
  • fetch all available glifs: GET https://glif.app/api/glifs
  • fetch featured glifs on homepage: GET https://glif.app/api/glifs?featured=1
  • fetch glifs created by a user (note: no username support yet): GET https://glif.app/api/glifs?userId=flkjasdflkjasdflkj
  • search - very basic, non-fuzzy: GET https://alpha.glif.xyz/api/glifs?query=FOO
User info
  • none at this time, just list of glifs (above)

CORS (Cross-origin requests)

These aren’t allowed by our API endpoints, largely so you don’t accidentally leak API keys on the client-side.
See below for example apps that handle this.

How to get an API key

Please contact us via email or Discord and we’ll hook you up
Soon you’ll be able to just generate keys on your /settings page

Example code and apps

NextJS

Here is an example NextJS app that has its own local /api/glif API endpoint:

Simple HTML + CSS (Replit)

Here is a simple pure HTML + CSS + vanilla javascript app hosted on Replit that you can fork and run right there: https://replit.com/@jamiedubs/FishShop

Cloudflare Worker

Since that repl app is pure HTML and doesn’t have its own API, we’re using a Cloudflare Worker serverless function to handle talking to the Glif API w/ CORS headers set:
The “Simple API” above is basically just this code with a shared API token.