Project

octoprint

0.0
There's a lot of open issues
Ruby client for OctoPrint's REST API covering files, jobs, printer state and commands, printer and slicing profiles, connection, access control, settings, timelapse, logs, system commands and more. Typed with Sorbet; API errors map to typed exceptions.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 7.1, < 9
>= 2.2, < 2.15
>= 1.0.3, < 1.3.0
>= 0.5.10908, < 0.7.0
~> 2.6
 Project Readme

OctoPrint Ruby Gem

A comprehensive, type-safe Ruby wrapper for OctoPrint's REST API with full Sorbet integration.

โœจ Key Features

  • ๐Ÿ›ก๏ธ Type Safety: Full Sorbet type annotations with compile-time error checking
  • ๐Ÿ”ฅ Smart Error Handling: HTTP status codes mapped to specific exception types
  • ๐Ÿ“ฆ Comprehensive API Coverage: Files, jobs, printer state and commands, printer profiles, connection, access control, settings, timelapse, logs, system commands and more
  • ๐Ÿงช Exceptional Quality: 100% line coverage, enforced in CI
  • ๐ŸŽฏ Multi-Printer Support: Manage multiple OctoPrint instances with thread-safe clients
  • โšก Modern Ruby: Built for Ruby 3.3+ with Zeitwerk autoloading and ActiveSupport integration

Installation

Gemfile installation

Add this line to your application's Gemfile:

gem 'octoprint'

or run in the terminal

bundle add octoprint

And then execute:

bundle install

global manual installation

gem install octoprint

Usage

require 'octoprint'

# Configure globally for single printer
Octoprint.configure(host: 'https://octopi.local/', api_key: 'your_api_key')

# List files with full type safety
result = Octoprint::Files.list                   # => Octoprint::Files
result.files.first.name                          # => String (typed)
result.files.first.type_path                     # => Array<String> (typed)

# Get current job with progress tracking
job = Octoprint::Job.get                         # => Octoprint::Job
puts "#{job.information.file[:name]}: #{job.progress.completion}%" if job.progress.completion

# Upload and start a print job (location defaults to Octoprint::Location::Local)
Octoprint::Files.upload("awesome_model.gcode")
Octoprint::Files.select(filename: "awesome_model.gcode")
Octoprint::Job.start

Or select and start printing in one call: Octoprint::Files.select(filename: "awesome_model.gcode", print: true).

To generate an app key on your Octoprint's instance, log in to it, click on the Settings button in the top menu and then go to the "Application Keys".

The gem always authenticates with this API key and doesn't keep cookies. To authenticate on the push API (WebSocket), get a session key with a passive login: Octoprint::Access::Users.login.session.

๐Ÿ›ก๏ธ Type Safety with Sorbet

This gem provides complete type safety with Sorbet integration:

# All models are fully typed
job = Octoprint::Job.get
job.information.file           # => Hash | nil (typed), e.g. job.information.file[:name]
job.progress.completion        # => Float | nil (typed)

Benefits:

  • Compile-time Error Detection: Catch errors before runtime with srb tc
  • IDE Integration: Full autocomplete and type information (requires proper IDE setup, for example: ruby-lsp and sorbet plugins for VSCode)
  • Refactoring Safety: Type-checked code changes across the entire codebase
  • No Runtime Overhead: Signatures are inline RBS comments (#: (String) -> Files), so nothing is type-checked at runtime
  • Ships its own RBIs: the rbi/ folder is included in the gem, so Tapioca picks up the types in apps that use Sorbet

๐Ÿ”ฅ Smart Error Handling

HTTP errors are automatically mapped to specific exception types for clean error flow:

begin
  Octoprint::Files.select(filename: "missing.gcode")
rescue Octoprint::Exceptions::NotFoundError => e
  # Handle 404 errors specifically
  puts "Resource not found: #{e.message}"
rescue Octoprint::Exceptions::BadRequestError => e
  # Handle 400 errors specifically
  puts "Invalid request: #{e.message}"
rescue Octoprint::Exceptions::AuthenticationError => e
  # Handle 403 errors specifically
  puts "Authentication failed: #{e.message}"
rescue Octoprint::Exceptions::Error => e
  # Catch-all for every error raised by the gem
  puts "OctoPrint error: #{e.message}"
end

Exception Types (all under Octoprint::Exceptions):

  • Error - Base class of every exception below, handy as a catch-all rescue
  • AuthenticationError (403) - Invalid API key or permissions
  • BadRequestError (400) - Invalid parameters or request format
  • NotFoundError (404) - Resource doesn't exist
  • ConflictError (409) - Resource conflicts or state issues
  • UnsupportedMediaTypeError (415) - Invalid file format
  • InternalServerError (500) - Server-side errors
  • UnknownError - Fallback for unexpected responses
  • ConnectionError - The server can't be reached (connection refused, unreachable host, ...)
  • TimeoutError - The server didn't respond within the configured timeouts (a subclass of ConnectionError)
  • MissingCredentialsError - Octoprint::Client.new was given a nil host or API key

๐Ÿ“ฆ Comprehensive API Coverage

This gem aims to provide complete coverage of OctoPrint's REST API:

Category Features
Files List, get, upload, delete, create folders, select/unselect, slice STL files, copy/move, download URLs
Jobs Current job info, progress tracking, start/cancel/restart/pause/resume
Printer Printer state, temperatures with history, print head jog/home, tool/bed/chamber temperatures, extrusion, SD card, arbitrary commands, custom controls, last error
Printer Profiles List, get, create, update, and delete printer profiles
Slicing List slicers and their slicing profiles; get, add, update, and delete slicing profiles
Connection Connect/disconnect printer, connection settings and options
Languages List, upload, and delete language packs
Logs List and delete log files, download URLs
Timelapse List and delete timelapses, render or delete unrendered ones, change the timelapse config
Access Users (list, add, get, update, delete, change password, user settings, API keys, current user, login (session key for the push API) and logout), groups (list, add, get, update, delete), permissions (list)
Settings Get and save settings, regenerate the API key, fetch templates, typed webcam settings
System Commands List commands (all or by source) and execute them
Wizard Get wizard data and finish wizards
Utilities Test paths, URLs, server connectivity, DNS resolution, and addresses
Server Server version (ServerVersion) and server status / safe mode (ServerInformation)
# File management
Octoprint::Files.upload("model.gcode", location: Octoprint::Location::Local)
Octoprint::Files.slice(filename: "model.stl", profile: { slicer: "cura" })

# Job control
job = Octoprint::Job.get
Octoprint::Job.start if job.state == "Operational"

# Printer control
printer = Octoprint::Printer.get(history: true, limit: 10)
printer.temperature.tool(0).actual        # => 214.8
Octoprint::Printer::Printhead.home(axes: %w[x y])
Octoprint::Printer::Tool.target(tool0: 220)
Octoprint::Printer::Bed.target(60)
Octoprint::Printer::Command.send_commands(["M106 S255", "M117 Hello"])

# Timelapses
Octoprint::Timelapse.get(unrendered: true).unrendered.each { |job| Octoprint::Timelapse.render(name: job.name) }
Octoprint::Timelapse.update_config(type: "timed", interval: 10, save: true)

# Printer profiles
profile = Octoprint::PrinterProfiles.create(
  profile: { id: "ender3", name: "Ender 3 Pro", model: "Ender 3 Pro" }
) # => Octoprint::PrinterProfiles::PrinterProfile

# Slicing profiles
Octoprint::Slicing.list[:curalegacy].profiles.keys # => [:high_quality]
Octoprint::Slicing.add_profile(slicer: "curalegacy", key: "draft", display_name: "Draft", data: { layer_height: 0.3 })
Octoprint::Slicing.get_profile(slicer: "curalegacy", key: "draft").data # => { layer_height: 0.3 }

# Connection management
Octoprint::Connection.connect(port: "/dev/ttyUSB0", baudrate: 115200)

Flexibility

This gem is built to offer multiple options to interact with Octoprint's apis. Depending on your use case, you can configure the gem for only one Octoprint server, or you can generate an API client to use for each server.

Example 1: Use in a Rails app, with a single Octoprint server

# config/initializers/octoprint.rb
Octoprint.configure(host: 'https://octopi.local/', api_key: 'j98G2nsJq...')

# app/controllers/printers_controller.rb
class PrintersController < ApplicationController
  def connect
    Octoprint::Connection.connect
  end
end

Example 2: Manage multiple printers in a ruby script

ender3 = Octoprint::Client.new(host: 'http://192.168.0.145', api_key: 'asdf')
cr10   = Octoprint::Client.new(host: 'http://192.168.0.167', api_key: 'ghjk')

files = []

ender3.use do
  files << Octoprint::Files.list
end

cr10.use do
  files << Octoprint::Files.list
end

Timeouts

By default, the client waits up to 2 seconds for the connection to open and up to 3 seconds for each read or write, so an offline printer can't hang your app. Pass open_timeout: and timeout: (in seconds) to Octoprint.configure or Octoprint::Client.new to change them. Raise timeout: for large file uploads. A nil timeout: falls back to the HTTP adapter's default (60 seconds for Net::HTTP), and a nil open_timeout: falls back to timeout:.

client = Octoprint::Client.new(host: 'http://192.168.0.145', api_key: 'asdf', open_timeout: 1, timeout: 30)

begin
  client.use { Octoprint::Job.get }
rescue Octoprint::Exceptions::ConnectionError # also rescues Octoprint::Exceptions::TimeoutError
  puts "Printer is offline"
end

Webcam settings

Octoprint::Settings.webcam returns the webcam settings as a typed object. It supports the multi-webcam settings of OctoPrint 1.9+ (using the default webcam) and the legacy settings of older versions.

webcam = Octoprint::Settings.webcam # => Octoprint::Settings::Webcam
webcam.enabled                      # => true
webcam.stream_url                   # => "/webcam/?action=stream" (can be relative to the host)
webcam.snapshot_url                 # => "http://localhost:8080/?action=snapshot"

# Already have the settings? Skip the extra request
Octoprint::Settings::Webcam.from_settings(Octoprint::Settings.get)

๐Ÿงช Testing & Code Quality

This gem maintains quality standards:

Test Coverage

  • 100% Line Coverage
  • API calls recorded with VCR: Real API interactions recorded and replayed, ensuring tests ran in real conditions while keeping CI simple and fast.

Code Quality

  • RuboCop
  • Sorbet Type Checking
# Run all quality checks
bundle exec rspec
bundle exec rubocop
bundle exec srb tc

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also 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. Releases are published from CI, see Releasing.

Testing with Octoprint

You can test with an existing octoprint instance that you have, or you can setup a test octoprint instance using the virtual printer plugin.

There are many ways you can set that up; using Docker, pip on your local machine or use a raspberry pi, and many more options. Pick the one that works for you.

As a convenience, here is the test setup I use, using Docker:

brew install --cask docker # Install docker on a mac, if not already installed.

# Once Docker is started
docker run -d -p 5050:80 --name octoprint octoprint/octoprint

Then, open http://localhost:5050/ and go through the setup wizard.

Enable the virtual printer plugin.

Go to Settings > Application keys and generate a new API key.

Set the OCTOPRINT_HOST_1_11 and OCTOPRINT_API_KEY_1_11 environment variables (and OCTOPRINT_HOST_2_0 / OCTOPRINT_API_KEY_2_0 to record against OctoPrint 2.0) in your preferred way. The older OCTOPRINT_HOST / OCTOPRINT_API_KEY still work for the 1.11 server. For convenience, there is an example .env that you can copy. Some development tools will recognize that file and automatically load its content as environment variables. Copy it by entering cp .env.example .env in your terminal and add your Octoprint's configuration to the newly created .env file. This file is ignored by git, so it is safe to edit. VCR is configured to filter that data out of cassettes.

Remember, you should never commit your credentials to git. With the current configuration, you should never have to.

Recorded HTTP requests for the test suite

This project uses VCR to record the HTTP interactions with an Octoprint's API. When you add tests, you can use existing cassettes or record new ones. You should never edit the cassettes manually. You can delete the files and re-record them as you need.

If you need all request to pass through while developing, you can add the :wip flag to your tests. It will prevent cassettes from recording until you remove the flag. Remember to remove it and record all missing cassettes before you commit your changes. More information here: https://link.medium.com/QU7ZgM8P9nb

example:

    it "calls the API", :wip, vcr: {cassette_name: '/currentuser'} do
      result = Octoprint::Access::User.current

      expect(result).to be_a Octoprint::Access::User
      expect(result.name).to eq 'sophie'
    end

It is preferable to have the test setup and teardown making all the required operations so deleting all cassettes to re-record them results in a successful full run, in any order of the tests.

For example: if a file is required on the octoprint instance in order to make an API call referencing that file, the test setup should upload it.

However, it is not realistic to apply this strictly, some exceptions exist. It is not recommended to delete all cassettes to record them again. Only re-record new cassettes if strictly needed.

Releasing

Publishing a GitHub Release for a vX.Y.Z tag runs .github/workflows/release.yml. It runs the lint and test workflows, checks that the tag matches Octoprint::VERSION, publishes the gem to rubygems.org through trusted publishing (no API key), replaces the release notes with the version's CHANGELOG section and attaches the .gem.

  1. Bump VERSION in lib/octoprint/version.rb.
  2. In CHANGELOG.md, move the entries under ## [X.Y.Z] - YYYY-MM-DD. The heading must contain [X.Y.Z] or the release stops before publishing.
  3. Open a PR and merge it into main.
  4. On GitHub, draft a new release: create the tag vX.Y.Z on main, and publish it. Saving a draft doesn't trigger anything.

The release is public before CI finishes. If lint, tests or the version and CHANGELOG checks fail, nothing is published: fix the problem on main, delete the release and its tag, and publish it again.

Don't run bundle exec rake release locally.

One-time setup, before the first release: on rubygems.org, add a pending trusted publisher (Profile โ†’ Trusted publishers) for gem octoprint, repository sophiedeziel/octoprint, workflow release.yml and environment rubygems. It becomes a regular trusted publisher after the first push.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/sophiedeziel/octoprint. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Octoprint project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.