Skip to content

lukefan/midjourney_api_package

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QianDuo MidJourney API Package

A Dart package that provides a simple and efficient way to interact with the MidJourney API. This package allows you to generate images, upscale them, and monitor task status through MidJourney's API service.

API Provider

This package is powered by QianDuoDuo's MidJourney API service. Before using this package, you need to:

  1. Register for an account at api.ifopen.ai
  2. Obtain your API token from the dashboard
  3. Use the default base URL: https://api.ifopen.ai

Features

  • 🎨 Generate images using text prompts
  • 🔍 Upscale generated images
  • 📊 Monitor task status and progress
  • 🔄 Automatic task polling
  • 🛡️ Error handling and type safety
  • 📝 Comprehensive logging

Installation

Add this package to your project's pubspec.yaml:

dependencies:
  qianduo_midjourney_api: ^1.0.3

Then run:

dart pub get

Usage

Complete Example

Here's a complete example of how to generate an image, monitor its progress, get the image URL, and upscale it:

void main() async {
  final api = MidjourneyApi(
    baseUrl: 'https://api.ifopen.ai',
    apiToken: 'YOUR_API_TOKEN',
  );

  try {
    // 1. Generate image
    final response = await api.imagine('A beautiful sunset over mountains');
    final taskId = response.result.toString();
    print('Generation started, task ID: $taskId');

    // 2. Monitor progress until completion
    final status = await api.pollTaskStatus(
      taskId,
      onProgress: (status, progress) {
        print('Status: $status, Progress: $progress');
      },
    );

    // 3. Get image URL when status is SUCCESS
    if (status.status == 'SUCCESS') {
      final imageUrl = api.getImageUrl(status.id);
      print('Image URL: $imageUrl');

      // 4. Get upscale buttons
      final buttons = api.getUpscaleButtons(status);
      print('Available upscale buttons:');
      for (var i = 0; i < buttons.length; i++) {
        print('U${i + 1}: taskId=${buttons[i]['taskId']}, customId=${buttons[i]['customId']}');
      }

      // 5. Upscale a specific version (e.g., U1)
      final upscaleResponse = await api.upscale(
        buttons[0]['taskId']!,
        buttons[0]['customId']!,
      );
      final upscaleTaskId = upscaleResponse.result.toString();

      // 6. Monitor upscale progress
      final upscaleStatus = await api.pollTaskStatus(
        upscaleTaskId,
        onProgress: (status, progress) {
          print('Upscale Status: $status, Progress: $progress');
        },
      );

      // 7. Get upscaled image URL
      if (upscaleStatus.status == 'SUCCESS') {
        final upscaledImageUrl = api.getImageUrl(upscaleStatus.id);
        print('Upscaled Image URL: $upscaledImageUrl');
      }
    }
  } catch (e) {
    print('Error: $e');
  } finally {
    api.dispose();
  }
}

Step-by-Step Guide

  1. Initialize the API Client:
final api = MidjourneyApi(
  baseUrl: 'https://api.ifopen.ai',
  apiToken: 'YOUR_API_TOKEN',
);
  1. Generate an Image:
final response = await api.imagine('Your prompt here');
final taskId = response.result.toString();
  1. Monitor Task Status:
final status = await api.pollTaskStatus(
  taskId,
  onProgress: (status, progress) {
    print('Status: $status, Progress: $progress');
  },
);
  1. Get Image URL:
if (status.status == 'SUCCESS') {
  final imageUrl = api.getImageUrl(status.id);
}
  1. Get Upscale Buttons:
final buttons = api.getUpscaleButtons(status);
// buttons is a list of maps containing taskId and customId for each U1-U4 button
// Example: [{'taskId': 'xxx', 'customId': 'yyy'}, ...]
  1. Upscale an Image:
// Upscale using U1 button (first button)
final upscaleResponse = await api.upscale(
  buttons[0]['taskId']!,
  buttons[0]['customId']!,
);
  1. Clean up:
api.dispose();

Error Handling

The package includes comprehensive error handling with specific exception types:

  • MidjourneyApiException: For API-related errors
  • MidjourneyJsonException: For JSON parsing errors
  • MidjourneyUpscaleException: For upscaling-related errors

Example error handling:

try {
  final response = await api.imagine('prompt');
} on MidjourneyApiException catch (e) {
  print('API Error: ${e.message}');
} on MidjourneyJsonException catch (e) {
  print('JSON Error: ${e.message}');
} catch (e) {
  print('Unexpected error: $e');
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Disclaimer

This package is not officially associated with MidJourney. Make sure you comply with MidJourney's terms of service and API usage guidelines when using this package.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages