npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@useapi/midjourney-api

v1.0.4

Published

TypeScript client library for Midjourney API by useapi.net

Downloads

1,952

Readme

@useapi/midjourney-api

This package contains TypeScript client library for Midjourney API by useapi.net.
Quick demo.

Install

npm

npm install @useapi/midjourney-api

Documentation

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 need to set up and configure the Midjourey Discord account as well as subscribe to useapi.net service before you can start using API. Setup instructions.

import {
    createConfiguration,
    DefaultApi,
    ImagineResponse,
    ButtonResponse,
    BlendResponse,
    JobResponse,
    JobsButtonPostRequestButtonEnum,
    JobsBlendPostRequestBlendDimensionsEnum,
    DescribeResponse,
} from '@useapi/midjourney-api';

const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms));

const configuration = createConfiguration({
    authMethods: {
        apiToken: {
            tokenProvider: {
                // Load api token from environment 
                getToken() { return process.env.USEAPI_TOKEN ?? 'useapi.net API token' }
            }
        }
    }
});

// Load Discord settings from environment 
const discord = process.env.USEAPI_DISCORD ?? 'Discord token';
const server = process.env.USEAPI_SERVER ?? 'Discord server id number';
const channel = process.env.USEAPI_CHANNEL ?? 'Discord channel id number';

const apiInstance = new DefaultApi(configuration);

// Helper function to monitor job status and retrieve results
const waitForJobToComplete = async (
    job: ImagineResponse |
        ButtonResponse |
        BlendResponse |
        DescribeResponse |
        JobResponse) => {

    const verb = job.verb.toUpperCase();

    console.log(`${verb} : ${job.status}`, job.jobid);

    while (job.code == 200 && ['started', 'progress'].includes(job.status)) {
        await sleep(20 * 1000);
        // JobResponse
        job = await apiInstance.jobsGet(job.jobid);
        console.log(`${verb} : ${job.status}`, { jobid: job.jobid, content: job.content });
    }

    if ((job instanceof JobResponse) && job.attachments?.length)
        console.log(`${verb} url`, job.attachments?.at(0)?.url);
    if ((job instanceof JobResponse) && job.buttons?.length)
        console.log(`${verb} buttons`, job.buttons?.join());

    return job;
}

const demo = async () => {
    // Midjourney /describe, using Promise pattern
    apiInstance.jobsDescribePost(
        {
            describeUrl: "https://mymodernmet.com/wp/wp-content/uploads/2017/12/free-images-national-gallery-of-art-9.jpg",
            discord,
            server,
            channel,
        }
    ).then(async (data: DescribeResponse) => {
        console.log('DESCRIBE started', data);
        await waitForJobToComplete(data);
    }).catch((error: any) => console.error('DESCRIBE failed', error));

    // Midjourney /blend, using Promise pattern
    apiInstance.jobsBlendPost(
        {
            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"
            ],
            blendDimensions: JobsBlendPostRequestBlendDimensionsEnum.Landscape,
            discord,
            server,
            channel,
        }
    ).then(async (data: BlendResponse) => {
        console.log('BLEND started', data);
        await waitForJobToComplete(data);
    }).catch((error: any) => console.error('BLEND failed', error));

    // Midjourney /imagine, using await & try..catch pattern
    let job: ImagineResponse | JobResponse | ButtonResponse;
    try {
        // ImagineResponse
        job = await apiInstance.jobsImaginePost(
            {
                prompt: "Steampunk cat cycling in San Francisco, vintage photo",
                discord,
                server,
                channel,
            });

        await waitForJobToComplete(job);

        const imagineJobId = job.jobid;

        // Midjourney button, using await & try..catch pattern
        job = await apiInstance.jobsButtonPost(
            {
                jobid: imagineJobId,
                button: JobsButtonPostRequestButtonEnum.V1
            });

        await waitForJobToComplete(job);
    } catch (ex: any) {
        console.log('IMAGINE or BUTTON failed', ex);
    }
}

demo();

Changelog

Version 1.0.4 | November 3, 2023

Added support for following jobs/button options:

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

Support

Visit our