In this example we will look into how to authenticate, generate and list images through Meteron.

You can find Python examples here: https://github.com/meteron-ai/python-example

Authentication

To authenticate, you will need to use the API key (you can find yours here https://app.meteron.ai/?tab=API Keys):

import os
import requests

METERON_API_KEY = os.getenv('METERON_API_KEY')

url = f'<https://app.meteron.ai/api/images/generations>'

headers = {
	"Content-Type": "application/json",
	"Authorization": "Bearer " + METERON_API_KEY,
}

Generate images

For image generation, you will need to have at least one Model defined. First, create it through the dashboard (follow the steps to “add AI model”):

import os
import requests

METERON_API_KEY = os.getenv('METERON_API_KEY')
METERON_MODEL = os.getenv('METERON_MODEL', "stable-diffusion")

url = "<https://app.meteron.ai/api/images/generations>"

payload = {"prompt": "futuristic city, extra detailed"}
headers = {
	"Content-Type": "application/json",
	"Authorization": "Bearer " + METERON_API_KEY,
	"X-Model": METERON_MODEL,
	"X-Async": "false",
  "X-User": "user-id-1"
}

response = requests.post(url, json=payload, headers=headers)

response_json = response.json()

# The outputImages URL contains a presigned URL from which you can
# download whatever your model has specified
print(response_json["outputImages"][0]["url"])

List images

When fetching images you will either need to specify pageSize (up to 200) or loop through the paginated response. Here’s a full example of iterating through the pages:

import os
import requests

METERON_API_KEY = os.getenv('METERON_API_KEY')
METERON_MODEL = os.getenv('METERON_MODEL', "stable-diffusion")

url = f'<https://app.meteron.ai/api/images/generations?model={METERON_MODEL}&pageSize={20}>'

headers = {
	"Content-Type": "application/json",
	"Authorization": "Bearer " + METERON_API_KEY,
}

generated_images = []
new_results = True
page_token = ''

'''
Response from the API is paginated. 
{
	"results": [
		...		
	],
	"pagination": {
		"pageSize": 20,
		"nextPageToken": "next-page-token-here",
		"previousPageToken": ""
	}
}
'''
while True:
  response = requests.get(url + f"&pageToken={page_token}", headers=headers).json()
  # Append the results
  new_results = response.get("results", [])  
  generated_images.extend(new_results)
  
  # Get the next page token
  page_token = response["pagination"].get('nextPageToken', '')
  
  # If there are no results, break the loop
  if not page_token:
    break

As long as the ["pagination"]["nextPageToken"] is defined, it’s safe to fetch the next page. Once you have exhausted all results, this entry will be empty and you shouldn’t expect more results there.

Enforce limits

You can enforce limits by configuring them in your cluster details page. If limits are configured, you will be required to send X-User header with each image generation request, i.e.:

payload = {"prompt": "futuristic city, extra detailed"}
headers = {
	"Content-Type": "application/json",
	"Authorization": "Bearer " + METERON_API_KEY,
	"X-Model": METERON_MODEL,
	"X-Async": "false",
  "X-User": "user-id-1"
}

response = requests.post(url, json=payload, headers=headers)