GitHub - Nidocq/MidJourneyAPI: Client to programmatically and automatically create and download images from Midjourney through Discord · GitHub
Skip to content

Nidocq/MidJourneyAPI

Repository files navigation

MidjourneyAPI

MidJourneyAPI is as its name suggests, an API for MidJourney as of now (14 june 23)
MidJourney has no API for public use.

This project is a personal project to make a tool that makes it quick and easy to use MidJourney in the terminal.

How it works

Your machine will create a session with Discord. MidjourneyAPI will then send messages to the Midjouney bot on behalf of you. Download the generated images and crop them into individual images. (Midjourney makes a 2x2 image from one prompt)

TODO:

Prerequisites

  • websockets library (pip3 install websockets)
  • Your keys (found in the 'Keys' folder)
    • API_channel_id.key
    • API_guild_id.key
    • API_session_id.key
    • DiscordServerID.key
    • authToken.key
  • A Discord Server that you can manage with Midjourney's bot joined

Development Design

classDiagram
    class Config{
        <<singleton>>
        - dict : Keys
        - dict : Global
        - dict : Env
        # ProductionType : chosenProd
        - String : session_id 
        - openFile(String : path) : String
        - parseStrJSON(String : str) : dict
        - loadKey(dict : srcDict, dict : dict : dict)
        - loadAllKeys(srcDict : dict) 
        + Key(String : keyName) : object
        + Global(String : keyName) : object
        + Env()
    }

    class NetworkManager{
        + WebsocketConnection wsConnection
        + POST(String : url, dict : headers, dict : data) : Response
        + MJsplit(String : discordImgUrl) : dict
        + MJdownloadPictures(dict : pictureUrls, String : downloadPath)
        + connectToWs(String : url)
        + generateSnowflake() : Int$
    }

    class IPacketAssembler{
        <<interface>>
        + createApplicationCommand() : dict
        + createPrelim() : dict
        + createDataPacket(String : prompt, String : includeImgURL) : dict
        + AssemblePacket(String : prompt, String : includeImgURL)
    }

    class PacketManager{
        AssembleImagine(String : prompt, List~String~ : includeImgUrl) : dict
    }
    class ImaginePacket {
        - uri_validator(String url) : bool
    }

    class InfoPacket {
    }

    class WebsocketConnection {
        + String reconnectURL
        + Int heartbeatInterval 
        + String session_id
        + int lastSeq
        + Connection cConnection
        + connect(String : url) : ClientConnection
        + maintainHeartbeat(int : heartbeatInterval)
    }

    class Opcodes{

    }
    class MidjourneyAPI {
        + Imagine(String : prompt, String : downloadPath)
    }
    class TerminalController {
        - Queue requestQueue 
        - String TerminalIcon
        + getInput(String : inputPromt) : String
    }

    PacketManager *-- MidjourneyAPI
    NetworkManager *-- PacketManager
    NetworkManager --* WebsocketConnection

    TerminalController  -->  MidjourneyAPI
    PacketManager --* ImaginePacket
    PacketManager --* InfoPacket
    InfoPacket  ..> IPacketAssembler 
    ImaginePacket ..> IPacketAssembler
Loading

this is an example of the request sent when you are connected to discord and whant to send an imagine command.
You will find the

  • guild_id : the first number after the 'channels/' in the URI
  • channel_id: : the last number in the URI.
  • API_session_id : just create the file and leave it empty
  • authToken : Fetched when a session with Discord is opened.

An example of Discords API payload, which is integrated in the terminal of your choice on your machine:

"payload_json":{
    "type": 2,
    "application_id": "936929561302675456",
    "guild_id": "",
    "channel_id": "",
    "session_id": "",
    "data": {
        "version": "1118961510123847772",
        "id": "938956540159881230",
        "name": "imagine",
        "type": 1,
        "options": [
            {
                "type": 3,
                "name": "prompt",
                "value": "minions walking in a line --q 0.25"
            }
        ],
        "application_command": {
            "id": "938956540159881230",
            "application_id": "936929561302675456",
            "version": "1118961510123847772",
            "default_member_permissions": null,
            "type": 1,
            "nsfw": false,
            "name": "imagine",
            "description": "Create images with Midjourney",
            "dm_permission": true,
            "contexts": [
                0,
                1,
                2
            ],
            "options": [
                {
                    "type": 3,
                    "name": "prompt",
                    "description": "The prompt to imagine",
                    "required": true
                }
            ]
        },
        "attachments": []
    },       
    "nonce": "1119669035626135552"
}

Developed by Phillip Lundin

About

Client to programmatically and automatically create and download images from Midjourney through Discord

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors