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.
This package is powered by QianDuoDuo's MidJourney API service. Before using this package, you need to:
- Register for an account at api.ifopen.ai
- Obtain your API token from the dashboard
- Use the default base URL:
https://api.ifopen.ai
- 🎨 Generate images using text prompts
- 🔍 Upscale generated images
- 📊 Monitor task status and progress
- 🔄 Automatic task polling
- 🛡️ Error handling and type safety
- 📝 Comprehensive logging
Add this package to your project's pubspec.yaml
:
dependencies:
qianduo_midjourney_api: ^1.0.3
Then run:
dart pub get
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();
}
}
- Initialize the API Client:
final api = MidjourneyApi(
baseUrl: 'https://api.ifopen.ai',
apiToken: 'YOUR_API_TOKEN',
);
- Generate an Image:
final response = await api.imagine('Your prompt here');
final taskId = response.result.toString();
- Monitor Task Status:
final status = await api.pollTaskStatus(
taskId,
onProgress: (status, progress) {
print('Status: $status, Progress: $progress');
},
);
- Get Image URL:
if (status.status == 'SUCCESS') {
final imageUrl = api.getImageUrl(status.id);
}
- 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'}, ...]
- Upscale an Image:
// Upscale using U1 button (first button)
final upscaleResponse = await api.upscale(
buttons[0]['taskId']!,
buttons[0]['customId']!,
);
- Clean up:
api.dispose();
The package includes comprehensive error handling with specific exception types:
MidjourneyApiException
: For API-related errorsMidjourneyJsonException
: For JSON parsing errorsMidjourneyUpscaleException
: 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');
}
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
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.