Skip to main content

Midjourney API by useapi.net

Project description

midjourney-api-client

This package contains Python client library for Midjourney API by useapi.net.

Useapi.net provides a simple, reliable and affordable way to use Midjourney via standard REST API.

Quick demo.

Requirements.

Python 3.7+

Installation & Usage

pip install

pip install midjourney-api-client

You may need to run pip with root permission: sudo pip install midjourney-api-client.

Then import the package:

import midjourney_api_client

API Overview

Midjourney /imagine command available via jobs/imagine API endpoint.

Midjourney upscale or create variations and enhance or modify button commands available via jobs/button API endpoint.

Midjourney /describe command available via jobs/describe API endpoint.

Midjourney /blend command available via jobs/blend API endpoint.

Use jobs/?jobid=jobid API endpoint to retrieve job results.

Postman collection.

Swagger OpenAPI documentation for generating server stubs and client SDKs.

Usage

You will need to set up and configure the Midjourey Discord account as well as subscribe to useapi.net service before you can start using API.

Please follow the installation procedure and then run the following:

import os
import time
from typing import Union
from pprint import pprint
import midjourney_api_client
from midjourney_api_client.api.default_api import DefaultApi
from midjourney_api_client.models.describe_response import DescribeResponse
from midjourney_api_client.models.imagine_response import ImagineResponse
from midjourney_api_client.models.job_response import JobResponse
from midjourney_api_client.models.jobs_blend_post_request import JobsBlendPostRequest
from midjourney_api_client.models.jobs_button_post_request import JobsButtonPostRequest
from midjourney_api_client.models.jobs_describe_post_request import JobsDescribePostRequest
from midjourney_api_client.models.jobs_imagine_post_request import JobsImaginePostRequest
from midjourney_api_client.rest import ApiException

configuration = midjourney_api_client.Configuration(
    host = "https://api.useapi.net/v1"
)

# Extract setting from environment
# Suggested shell execution command:
# USEAPI_TOKEN="..." USEAPI_DISCORD="..." USEAPI_SERVER="..." USEAPI_CHANNEL="..." python3 ./test.py
api_token = os.environ["USEAPI_TOKEN"]
discord = os.environ["USEAPI_DISCORD"]
server = os.environ["USEAPI_SERVER"]
channel = os.environ["USEAPI_CHANNEL"]
# Optional callback url
# We recommend using sites like webhook.site to test callback URL functionality.
reply_url = os.environ.get("USEAPI_CALLBACK", "")

# Configure Bearer authorization: apiToken
configuration = midjourney_api_client.Configuration(
    access_token = api_token
)

# This function is not not needed if job parameter reply_url (callback) specified
def wait_for_job_to_complete(api_instance: DefaultApi, job: Union[DescribeResponse, ImagineResponse, JobResponse]):
    verb = job.verb.upper()
    print(f"{verb} : {job.status}", job.jobid)

    while job.code == 200 and job.status in ['started', 'progress']:
        # Sleep for 20 seconds
        time.sleep(20)  
        job = api_instance.jobs_get(job.jobid)
        print(f"{verb} : {job.status}", {"jobid": job.jobid, "content": job.content})

    if isinstance(job, JobResponse) and job.attachments:
        print(f"{verb} url", job.attachments[0].url)
    if isinstance(job, JobResponse) and job.buttons:
        print(f"{verb} buttons", ", ".join(job.buttons))

    return job

def main():
    # Enter a context with an instance of the API client
    with midjourney_api_client.ApiClient(configuration) as api_client:
        # Create an instance of the API class
        api_instance = midjourney_api_client.DefaultApi(api_client)

        # Midjourney /describe
        try:
            jobs_describe_post_request = JobsDescribePostRequest(
                describeUrl="https://mymodernmet.com/wp/wp-content/uploads/2017/12/free-images-national-gallery-of-art-9.jpg",
                discord=discord,
                server=server,
                channel=channel,
                reply_url=reply_url
            )
            describe_response = api_instance.jobs_describe_post(jobs_describe_post_request)
            pprint(describe_response)

            # Not needed if job parameter reply_url (callback) specified
            describe_response = wait_for_job_to_complete(api_instance, describe_response)

        except ApiException as e:
            print("Exception when calling jobs_describe_post_request: %s\n" % e)

        # Midjourney /blend
        try:
            jobs_blend_post_request = JobsBlendPostRequest(
                blendUrls = [
                    "https://mymodernmet.com/wp/wp-content/uploads/2017/12/free-images-national-gallery-of-art-6.jpg",
                    "https://mymodernmet.com/wp/wp-content/uploads/2017/12/free-images-national-gallery-of-art-2.jpg"
                ],
                discord=discord,
                server=server,
                channel=channel,
                reply_url=reply_url
            )
            blend_response = api_instance.jobs_blend_post(jobs_blend_post_request)
            pprint(blend_response)

            # Not needed if job parameter reply_url (callback) specified
            blend_response = wait_for_job_to_complete(api_instance, blend_response)

        except ApiException as e:
            print("Exception when calling jobs_blend_post: %s\n" % e)

        # Midjourney /imagine
        try:
            jobs_imagine_post_request = JobsImaginePostRequest(
                prompt="Steampunk cat cycling in San Francisco, vintage photo",
                discord=discord,
                server=server,
                channel=channel,
                reply_url=reply_url
            )
            imagine_response = api_instance.jobs_imagine_post(jobs_imagine_post_request)
            pprint(imagine_response)

            imagine_response = wait_for_job_to_complete(api_instance, imagine_response)

        except ApiException as e:
            print("Exception when calling jobs_imagine_post: %s\n" % e)

        # Midjourney button commands
        try:
            jobs_button_post_request = JobsButtonPostRequest(
                button="V1",
                jobid=imagine_response.jobid,
                reply_url=reply_url
            )
            button_response = api_instance.jobs_button_post(jobs_button_post_request)
            pprint(button_response)

            # Not needed if job parameter reply_url (callback) specified
            button_response = wait_for_job_to_complete(api_instance, button_response)

        except ApiException as e:
            print("Exception when calling jobs_button_post: %s\n" % e)

main()

Documentation for API Endpoints

All URIs are relative to https://api.useapi.net/v1

Method HTTP request Description
jobs_imagine_post POST /jobs/imagine Midjourney /imagine command
jobs_blend_post POST /jobs/blend Midjourney /blend command
jobs_describe_post POST /jobs/describe Midjourney /describe command
jobs_button_post POST /jobs/button Midjourney upscale or create variations and enhance or modify buttons
jobs_get GET /jobs/ Retrieve job status and results
jobs_get_list GET /jobs Get list of currently executing jobs
jobs_cancel_get GET /jobs/cancel/ Cancel job
account_get GET /account Retrieve useapi.net account information

Documentation For Models

Documentation For Authorization

Authentication schemes defined for the API:

apiToken

  • Type: Bearer authentication

Changelog

Version 1.0.1 | November 3, 2023

Added support for following jobs/button options:

  • Upscale (2x)
  • Upscale (4x)
  • Redo Upscale (2x)
  • Redo Upscale (4x)

Support

Visit our

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

midjourney-api-client-1.0.1.tar.gz (33.8 kB view details)

Uploaded Source

Built Distribution

midjourney_api_client-1.0.1-py3-none-any.whl (48.8 kB view details)

Uploaded Python 3

File details

Details for the file midjourney-api-client-1.0.1.tar.gz.

File metadata

  • Download URL: midjourney-api-client-1.0.1.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for midjourney-api-client-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d29c13efc120cef3250450ca3272277e58e6b7a42d68f5490502012de742bf39
MD5 ebe64b7a2f9b4b1ffcb0cff336a58a9d
BLAKE2b-256 4ba784af2fb6d0b90c49ddc9690704b6819006f5c8990aed9c495b4401f8456d

See more details on using hashes here.

File details

Details for the file midjourney_api_client-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for midjourney_api_client-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 04c2b3675bd8376d6f54310ea90b93ef343818a535b0c1969b5d314b1f7d9111
MD5 b83efd953e575141c0d00899726c28eb
BLAKE2b-256 9da34ab6c60a310f56c0ebcdb75db626810a15894d69115b2219c85613cdd38f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page