Skip to content
Rbrtcs1 edited this page Oct 12, 2023 · 6 revisions

API

The API Docs for AI Horde are available at the below URL (you can click the header that tells the API Version to view endpoints):
https://aihorde.net/api/

you need to set the Client-Agent — which uniquely identifies your integration — to the request headers. the format for Client-Agent is client_name:version:contact_details, make sure your contact details are correct as we may contact you in case there is an issue with your integration.

If you want to use a negative prompt, you need to add it to the prompt, separated by "###" For example, in "cat###black" the positive prompt is "cat" and the negative prompt is "black"

some common endpoints you may need are written below:

  • /v2/generate/async

    You can use this endpoint to start the generation of an image.

  • /v2/generate/check

    To check the status of a request.

  • /v2/generate/status

    To retrieve the generated images. or to delete a request if DELETE http method is used.

you can also look at some of the open source clients in case you need to know how to implement certain functionality.

you might need a list of all available models on AI Horde to show your users, in that case you can get the json here.

Considerations

  • If you're developing a paid integration on top of AI Horde, it is okay as long as you give back to the AI Horde by running your own worker, more about that here.

  • also make sure to use the "n" parameter to request images as requesting every image individually can put unnecessary load on the AI Horde servers.

  • it is recommended to convert images you submit to WebP, to reduce the bandwidth usage.

  • in case the user cancels the image generation or the program crashes or for any other reason you wouldn't retrieve the image, make sure to cancel the request.

Libraries

Horde SDK - A Python library for AI Horde API

@zeldafan0225/ai_horde - An NPM Package

Code Snippets

Example scripts to demonstrate usage of the API

Python

import requests
import time

GENERATE_URL = "https://aihorde.net/api/v2/generate/async"
CHECK_URL = "https://aihorde.net/api/v2/generate/check/"
STATUS_URL = "https://aihorde.net/api/v2/generate/status/"
data = {
    "prompt": "landscape ### blurry",
    "params": {
        "cfg_scale": 9,
        "seed": "42",
        "sampler_name": "k_euler",
        "height": 512,
        "width": 512,
        "steps": 20,
        "tiling": False,
        "karras": True,
        "clip_skip": 1,
        "n": 2
    },
    "nsfw": True,
    "censor_nsfw": False,
    "trusted_workers": True,
    "models": ["Deliberate"], 
    "r2": True,
    "replacement_filter": True,
    "shared": False,
    "slow_workers": False,
    "dry_run": False
}

headers = {
    "apikey": "0000000000"
}


response = requests.post(GENERATE_URL, headers=headers, json = data).json()

print(response)

_id = response["id"]

while True:
    response = requests.get(CHECK_URL + _id, headers = headers).json()
    print(response)
    if response["finished"] == data["params"]["n"]:
        break
    time.sleep(2)

response = requests.get(STATUS_URL + _id, headers = headers).json()

print(response)
Clone this wiki locally