# 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: "get_printer_info", id: }` - Returns:\ An object containing the build version, cpu info, and if the Klippy process is ready for operation. The latter is useful when a client connects after the klippy state event has been broadcast. `{version: "", cpu: "", is_ready: , hostname: "", error_detected: , message: ""}` ### Emergency Stop - HTTP command:\ `POST /printer/emergency_stop` - Websocket command:\ `{jsonrpc: "2.0", method: "post_printer_emergency_stop", id: }` - Returns:\ `ok` ### Restart the host - HTTP command:\ `POST /printer/restart` - Websocket command:\ `{jsonrpc: "2.0", method: "post_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: "post_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: "get_printer_objects_list", id: }` - Returns:\ An object containing key, value pairs, where the key is the name of the Klippy module available for status query, and the value is an array of strings containing that module's available attributes. ```json { gcode: ["busy", "gcode_position", ...], toolhead: ["position", "status"...], ...} ``` ### Request currently subscribed objects: - HTTP command: `GET /printer/objects/subscription` - Websocket command:\ `{jsonrpc: "2.0", method: "get_printer_objects_subscription", id: }` - Returns:\ An object of the similar that above, however the format of the `result` value is changed to include poll times: ```json { objects: { gcode: ["busy", "gcode_position", ...], toolhead: ["position", "status"...], ...}, poll_times: { gcode: .25, toolhead: .25, ...} } ``` ### Request the a status update for an object, or group of objects: - HTTP command:\ `GET /printer/objects/status?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: "get_printer_objects_status", params: {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 keys are the requested Klippy objects, as shown below: ```json { 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/subscription?gcode=gcode_position,bus&extruder=target` - Websocket command:\ `{jsonrpc: "2.0", method: "post_printer_objects_subscription", params: {gcode: [], toolhead: ["position", "status"]}, id: }` - Returns:\ An acknowledgement that the request has been received: `ok` The actual status updates will be sent asynchronously over the websocket. ### Query Endstops - HTTP command:\ `GET /printer/query_endstops/status` - Websocket command:\ `{jsonrpc: "2.0", method: "get_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: "get_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: "post_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: "get_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: "post_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: "post_printer_print_pause", id: }` - Returns:\ `ok` ### Resume a print - HTTP command:\ `POST /printer/print/resume` - Websocket command:\ `{jsonrpc: "2.0", method: "post_printer_print_resume", id: }` - Returns:\ `ok` ### Cancel a print - HTTP command:\ `POST /printer/print/cancel` - Websocket command:\ `{jsonrpc: "2.0", method: "post_printer_print_cancel", id: }` - Returns:\ `ok` ## Machine Commands ### Shutdown the Operating System - HTTP command:\ `POST /machine/shutdown` - Websocket command:\ `{jsonrpc: "2.0", method: "post_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: "post_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 user has specified a folder in which "included" config files reside. If the user is not using "include" functionality, or if they have not specified the folder in moonraker's configuration, then the "config" root will not exist. Also note that while `printer.cfg` is part of the "config" root for the purposes of uploading, it should not reside in the same folder as included files, thus it will not show up for file list or directory queries. ### 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: "get_file_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: "get_file_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: