Skip to content

Instantly share code, notes, and snippets.

@MinusGix
Last active October 18, 2022 14:19
Show Gist options
  • Save MinusGix/bd69a578b049672795de9f7f3e6f7122 to your computer and use it in GitHub Desktop.
Save MinusGix/bd69a578b049672795de9f7f3e6f7122 to your computer and use it in GitHub Desktop.
Nai imagegen api

The basic request isn't complicated, so you can grab some library in your favorite language and just do it.
Obviously, making requests cost Anlas based on your subscription (so, the below will generate within the free Anlas for Opus subscriptions, but if you make the image large or increase steps a lot then it won't!)
It is basically just: A POST request to https://api.novelai.net/ai/generate-image The body of the request should be JSON, in the form of:

{
    // The input to the prompt.
    // Note that this doesn't automatically include the 'masterpiece, best quality' that the website prepends! You have to add that yourself.
    "input": "toaster, rgb, gaming, neon, realistic",
    // The ai model to use
    // This is the 'Anime (full)', I forget what the curated version is called
    // The furry version is `nai-diffusion-furry`
    "model": "nai-diffusion",
    "parameters": {
        // The random seed for the image.
        // Not sure if this must be a number?
        "seed": 525829572,
        // Undesired content
        "uc": "microwave",
        // The selected preset
        // However, they include the text on the client side, so this might just be so they can store your previous settings?
        "ucPreset": 0,
        // Obviously, the dimensions of the image. This is for the normal portrait.
        // If you open the dropdown on the web generator, then you'll see the various sizes.
        "height": 768,
        "width": 512,

        // The number of samples to generate, same as the slider
        "n_samples": 1,
        // Same as in ui
        "sampler": "k_euler_ancestral",
        "scale": 11,
        "steps": 28, 

        // These only appear in enhance or img2img ui I think, but they seem to be sent anyway?
        "noise": 0.2,
        "strength": 0.7,
    }
}

Default undesired content, I think, though I forget if I modified this slightly: nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry

You'll need to include the Authorization header in the request, which just authorizes you as your account. You can get the api key by snooping in the localstorage for novelai.net. It is in the form Authorization: Bearer {your api key here}
I also add Content-Type: application/json and accept: application/json, though I haven't tested whether those are strictly required.

The response is using some event sources api. I just ignored that and waited for the whole response, which was something like:

event: newImage
id: 1
data:{bunch of base64 here}

I presume it is multiple if you generate more images at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment