# API Most API methods are supported over both the Websocket and HTTP transports. File Transfer and "/access" requests are only available over HTTP. The Websocket is required to receive printer generated events such as gcode responses. For information on how to set up the Websocket, please see the Appendix at the end of this document. Note that all HTTP responses are returned as a json encoded object in the form of: `{result: }` The command matches the original command request, the result is the return value generated from the request. Websocket requests are returned in JSON-RPC format: `{jsonrpc: "2.0", "result": , id: }` HTML requests will recieve a 500 status code on error, accompanied by the specific error message. Websocket requests that result in an error will receive a properly formatted JSON-RPC response: `{jsonrpc: "2.0", "error": {code: , message: }, id: }` Note that under some circumstances it may not be possible for the server to return a request ID, such as an improperly formatted json request. The `test\client` folder includes a basic test interface with example usage for most of the requests below. It also includes a basic JSON-RPC implementation that uses promises to return responses and errors (see json-rcp.js). ## Printer Administration ### Get Klippy host information: - HTTP command:\ `GET /printer/info` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.info", id: }` - Returns:\ An object containing the build version, cpu info, Klippy's current state. ```json { state: "", state_message: "", hostname: "", software_version: "", cpu_info: "", klipper_path: "", python_path: "", log_file: "", config_file: "", } ``` ### Emergency Stop - HTTP command:\ `POST /printer/emergency_stop` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.emergency_stop", id: }` - Returns:\ `ok` ### Restart the host - HTTP command:\ `POST /printer/restart` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.restart", id: }` - Returns:\ `ok` ### Restart the firmware (restarts the host and all connected MCUs) - HTTP command:\ `POST /printer/firmware_restart` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.firmware_restart", id: }` - Returns:\ `ok` ## Printer Status ### Request available printer objects and their attributes: - HTTP command:\ `GET /printer/objects/list` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.objects.list", id: }` - Returns:\ An a list of "printer objects" that are currently available for query or subscription. This list will be passed in an "objects" parameter. ```json { objects: ["gcode", "toolhead", "bed_mesh", "configfile",....]} ``` ### Query the a status for an object, or group of objects: - HTTP command:\ `GET /printer/objects/query?gcode` The above will fetch a status update for all gcode attributes. The query string can contain multiple items, and specify individual attributes: `?gcode=gcode_position,busy&toolhead&extruder=target` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.objects.query", params: {objects: {gcode: [], toolhead: ["position", "status"]}}, id: }` Note that an empty array will fetch all available attributes for its key. - Returns:\ An object where the top level items are "eventtime" and "status". The "status" item contains data about the requested update. ```json { eventtime: , status: { gcode: { busy: true, gcode_position: [0, 0, 0 ,0], ...}, toolhead: { position: [0, 0, 0, 0], status: "Ready", ...}, ...} } ``` ### Subscribe to a status request or a batch of status requests: - HTTP command:\ `POST /printer/objects/subscribe?gcode=gcode_position,bus&extruder=target` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.objects.subscribe", params: {objects: {gcode: [], toolhead: ["position", "status"]}}, id: }` - Returns:\ Status data for all currently subscribed objects, with the format matching that of the `/printer/objects/query`: ```json { eventtime: , status: { gcode: { busy: true, gcode_position: [0, 0, 0 ,0], ...}, toolhead: { position: [0, 0, 0, 0], status: "Ready", ...}, ...} } ``` Note that Moonraker's current behavior is maintain a superset of all client subscriptions, thus you may received data for objects that you did not request. This behavior is subject to change in the future (where each client receives only the subscriptions it requested). Future updates for subscribed objects are sent asynchronously over the websocket. See the `notify_status_update` notification for details. ### Query Endstops - HTTP command:\ `GET /printer/query_endstops/status` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.query_endstops.status", id: }` - Returns:\ An object containing the current endstop state, with each attribute in the format of `endstop:`, where "state" can be "open" or "TRIGGERED", for example: ```json {x: "TRIGGERED", y: "open", z: "open"} ``` ### Fetch stored temperature data - HTTP command:\ `GET /server/temperature_store` - Websocket command: `{jsonrpc: "2.0", method: "server.temperature_store", id: }` - Returns:\ An object where the keys are the available temperature sensor names, and with the value being an array of stored temperatures. The array is updated every 1 second by default, containing a total of 1200 values (20 minutes). The array is organized from oldest temperature to most recent (left to right). Note that when the host starts each array is initialized to 0s. ## Gcode Controls ### Run a gcode: - HTTP command:\ `POST /printer/gcode/script?script=` For example,\ `POST /printer/gcode/script?script=RESPOND MSG=Hello`\ Will echo "Hello" to the terminal. - Websocket command:\ `{jsonrpc: "2.0", method: "printer.gcode.script", params: {script: }, id: }` - Returns:\ An acknowledgement that the gcode has completed execution: `ok` ### Get GCode Help - HTTP command:\ `GET /printer/gcode/help` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.gcode.help", params: {script: }, id: }` - Returns:\ An object where they keys are gcode handlers and values are the associated help strings. Note that help strings are not available for basic gcode handlers such as G1, G28, etc. ## Print Management ### Print a file - HTTP command:\ `POST /printer/print/start?filename=` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.print.start", params: {filename: , id:}` - Returns:\ `ok` on success ### Pause a print - HTTP command:\ `POST /printer/print/pause` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.print.pause", id: }` - Returns:\ `ok` ### Resume a print - HTTP command:\ `POST /printer/print/resume` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.print.resume", id: }` - Returns:\ `ok` ### Cancel a print - HTTP command:\ `POST /printer/print/cancel` - Websocket command:\ `{jsonrpc: "2.0", method: "printer.print.cancel", id: }` - Returns:\ `ok` ## Machine Commands ### Shutdown the Operating System - HTTP command:\ `POST /machine/shutdown` - Websocket command:\ `{jsonrpc: "2.0", method: "machine.shutdown", id: }` - Returns:\ No return value as the server will shut down upon execution ### Reboot the Operating System - HTTP command:\ `POST /machine/reboot` - Websocket command:\ `{jsonrpc: "2.0", method: "machine.reboot", id: }` - Returns:\ No return value as the server will shut down upon execution ## File Operations Most file operations are available over both APIs, however file upload, file download, and file delete are currently only available via HTTP APIs. Moonraker organizes different local directories into "roots". For example, gcodes are located at `http:\\host\server\files\gcodes\*`, otherwise known as the "gcodes" root. The following roots are available: - gcodes - config - config_examples (read-only) Write operations (upload, delete, make directory, remove directory) are only available on the `gcodes` and config roots. Note that the `config` root is only available if the "config_path" option has been set in Moonraker's configuration. ### List Available Files Walks through a directory and fetches all files. All file names include a path relative to the specified "root". Note that if the query st - HTTP command:\ `GET /server/files/list?root=gcodes` If the query string is omitted then the command will return the "gcodes" file list by default. - Websocket command:\ `{jsonrpc: "2.0", method: "server.files.list", params: {root: "gcodes"} , id: }` If `params` are are omitted then the command will return the "gcodes" file list. - Returns:\ A list of objects containing file data in the following format: ```json [ {filename: "file name", size: , modified: "last modified date", ...] ``` ### Get GCode Metadata Get file metadata for a specified gcode file. If the file is located in a subdirectory, then the file name should include the path relative to the "gcodes" root. For example, if the file is located at:\ `http://host/server/files/gcodes/my_sub_dir/my_print.gcode` Then the filename should be `my_sub_dir/my_print.gcode`. - HTTP command:\ `GET /server/files/metadata?filename=` - Websocket command:\ `{jsonrpc: "2.0", method: "server.files.metadata", params: {filename: "filename"} , id: }` - Returns:\ Metadata for the requested file if it exists. If any fields failed parsing they will be omitted. The metadata will always include the file name, modified time, and size. ```json { filename: "file name", size: , modified: "last modified date", slicer: "Slicer Name", first_layer_height: , layer_height: , object_height: , estimated_time: