Use Midjourney with Ruby! ๐ค๐
Using SlashImagine as the API until Midjourney releases their official API (at which point I'll swap it over to that). Please note that this library is unofficial and not affiliated with Midjourney or SlashImagine!
For now you'll need some SlashImagine credits to use this library.
๐ฎ Ruby AI Builders Discord | ๐ฆ Twitter | ๐ค OpenAI Gem | ๐ง Anthropic Gem
Add this line to your application's Gemfile:
gem "midjourney"
And then execute:
$ bundle install
Or install with:
$ gem install midjourney
and require with:
require "midjourney"
- Get your API key from https://slashimagine.pro/ - click Pricing, sign up to a plan, and then click My Account to see your API key.
For a quick test you can pass your api key directly to a new client:
client = Midjourney::Client.new(access_token: "access_token_goes_here")
For a more robust setup, you can configure the gem with your API keys, for example in an midjourney.rb
initializer file. Never hardcode secrets into your codebase - instead use something like dotenv to pass the keys safely into your environments.
Midjourney.configure do |config|
config.access_token = ENV.fetch("MIDJOURNEY_API_KEY")
end
Then you can create a client like this:
client = Midjourney::Client.new
The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the request_timeout
when initializing the client.
client = Midjourney::Client.new(
access_token: "access_token_goes_here",
request_timeout: 240 # Optional
)
You can also set these keys when configuring the gem:
Midjourney.configure do |config|
config.access_token = ENV.fetch("MIDJOURNEY_API_KEY")
config.request_timeout = 240 # Optional
end
Hit the Midjourney API for an image:
response = client.imagine(
parameters: {
prompt: "A stunning forest beneath a bright blue sky",
})
puts response["taskId"]
# => "abc123"
You can use this taskId to check the status of the task and get your image:
response = client.result(
parameters: {
taskId: "abc123",
})
puts response["imageURL"]
# => "https://....png"
After checking out the repo, run bin/setup
to install dependencies. You can run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
.
If you have an MIDJOURNEY_API_KEY
in your ENV
, running the specs will use this to run the specs against the actual API, which will be slow and cost you money - 3 credits or more! Remove it from your environment with unset
or similar if you just want to run the specs against the stored VCR responses.
First run the specs without VCR so they actually hit the API. This will cost 2 cents or more. Set MIDJOURNEY_API_KEY in your environment or pass it in like this:
MIDJOURNEY_API_KEY=123abc bundle exec rspec
Then update the version number in version.rb
, update CHANGELOG.md
, run bundle install
to update Gemfile.lock, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/alexrudall/midjourney. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Ruby Midjourney project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.