JavaScript Client Library
The JS Client Library provides an interface to all of OctoPrint’s API, including the SockJS based socket to send
push messages from the server to connected clients. It is available as packed web
asset file at /static/webassets/packed_client.js
or as individual
component files at /static/js/app/client/<component>.js
relative to your
OctoPrint instance’s base URL (e.g. http://octopi.local/static/webassets/packed_client.js
).
If you are using it from a web page hosted on OctoPrint as a Jinja2 template, you should use one of the following methods to embed it instead of manually entering the URL, in order to have OctoPrint take care of setting the correct URL prefix:
<!--
full client library or all individual files, depending
on the server mode - should be the preferred variant
-->
{% assets "js_client" %}<script type="text/javascript" src="{{ ASSET_URL }}"></script>{% endassets %}
<!--
full client library
-->
<script type="text/javascript" src="{{ url_for("static", filename="webassets/packed_client.js") }}"></script>
<!--
individual components (do not forget base!)
-->
<script type="text/javascript" src="{{ url_for("static", filename="js/app/client/<component>.js") }}"></script>
Regardless of which way you use to include the library, you’ll also need to make sure you included jQuery and lodash,
because the library depends on those to be available (as $
and _
). You can embed those like this:
<script src="{{ url_for("static", filename="js/lib/jquery/jquery.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/lodash.min.js") }}"></script>
Moreover, if you need to use the socket module, you’ll need to include sockJS. You can embed it like this:
<script src="{{ url_for("static", filename="js/lib/sockjs.min.js") }}"></script>
Note that all components depend on the base
component to be present, so if you are only including a select
number of components, make sure to at the very least include that one to be able to utilize the client.
When you import the client library as described above, a global variable OctoPrint
will become available, which is
a prepared instance of the OctoPrintClient
class the library assembles from registered components. You can directly
use that singular OctoPrint
instance if you only need to talk to one OctoPrint server:
OctoPrint.options.baseurl = "http://example.com/octoprint/"
OctoPrint.options.apikey = "apikey";
OctoPrint.files.list()
.done(function(response) {
// do something with the response
});
If you want to access multiple servers, you should however instead instantiate your own clients. You can provide the
connection options (baseurl
and apikey
) directly in the constructor or set them later:
var client1 = new OctoPrintClient({baseurl: "http://example.com/octoprint1/", apikey: "apikey1"});
var client2 = new OctoPrintClient();
client2.options.baseurl = "http://example.com/octoprint2/";
client2.options.apikey = "apikey2";
client1.files.list()
.done(function(response) {
// do something with the response for server 1
});
client2.files.list()
.done(function(response) {
// do something with the response for server 2
});
See also
- Application Key Plugin
A bundled plugin that implements an authorization workflow for third party clients. It adds various additional methods to the JS Client Library.
OctoPrintClient
OctoPrintClient
OctoPrintClient.options
OctoPrintClient.plugins
OctoPrintClient.getBaseUrl()
OctoPrintClient.getCookie()
OctoPrintClient.getRequestHeaders()
OctoPrintClient.ajax()
OctoPrintClient.ajaxWithData()
OctoPrintClient.get()
OctoPrintClient.getWithQuery()
OctoPrintClient.post()
OctoPrintClient.postJson()
OctoPrintClient.put()
OctoPrintClient.putJson()
OctoPrintClient.patch()
OctoPrintClient.patchJson()
OctoPrintClient.delete()
OctoPrintClient.download()
OctoPrintClient.upload()
OctoPrintClient.issueCommand()
OctoPrintClient.getSimpleApiUrl()
OctoPrintClient.simpleApiGet()
OctoPrintClient.simpleApiCommand()
OctoPrintClient.getBlueprintUrl()
OctoPrintClient.createRejectedDeferred()
OctoPrintClient.createCustomException()
OctoPrintClient.registerPluginComponent()
OctoPrintClient.InvalidArgumentError
OctoPrintClient.browser
OctoPrintClient.connection
OctoPrintClient.control
OctoPrintClient.files
OctoPrintClient.files.get()
OctoPrintClient.files.list()
OctoPrintClient.files.listForLocation()
OctoPrintClient.files.select()
OctoPrintClient.files.slice()
OctoPrintClient.files.delete()
OctoPrintClient.files.copy()
OctoPrintClient.files.move()
OctoPrintClient.files.createFolder()
OctoPrintClient.files.upload()
OctoPrintClient.files.download()
OctoPrintClient.files.pathForEntry()
OctoPrintClient.files.entryForPath()
OctoPrintClient.job
OctoPrintClient.languages
OctoPrintClient.logs
OctoPrintClient.printer
OctoPrintClient.printer.getFullState()
OctoPrintClient.printer.getToolState()
OctoPrintClient.printer.setToolTargetTemperatures()
OctoPrintClient.printer.setToolTemperatureOffsets()
OctoPrintClient.printer.selectTool()
OctoPrintClient.printer.extrude()
OctoPrintClient.printer.setFlowrate()
OctoPrintClient.printer.getBedState()
OctoPrintClient.printer.setBedTargetTemperature()
OctoPrintClient.printer.setBedTemperatureOffset()
OctoPrintClient.printer.getChamberState()
OctoPrintClient.printer.setChamberTargetTemperature()
OctoPrintClient.printer.setChamberTemperatureOffset()
OctoPrintClient.printer.jog()
OctoPrintClient.printer.home()
OctoPrintClient.printer.setFeedrate()
OctoPrintClient.printer.getSdState()
OctoPrintClient.printer.initSd()
OctoPrintClient.printer.refreshSd()
OctoPrintClient.printer.releaseSd()
OctoPrintClient.printerprofiles
OctoPrintClient.settings
OctoPrintClient.slicing
OctoPrintClient.socket
OctoPrintClient.socket.options
OctoPrintClient.socket.connect()
OctoPrintClient.socket.reconnect()
OctoPrintClient.socket.disconnect()
OctoPrintClient.socket.onMessage()
OctoPrintClient.socket.removeMessage()
OctoPrintClient.socket.sendMessage()
OctoPrintClient.socket.sendAuth()
OctoPrintClient.socket.onRateTooLow()
OctoPrintClient.socket.onRateTooHigh()
OctoPrintClient.socket.increaseRate()
OctoPrintClient.socket.decreaseRate()
- Sample to setup an authed socket
- Communication Throttling
OctoPrintClient.system
OctoPrintClient.timelapse
OctoPrintClient.timelapse.get()
OctoPrintClient.timelapse.list()
OctoPrintClient.timelapse.listRendered()
OctoPrintClient.timelapse.listUnrendered()
OctoPrintClient.timelapse.download()
OctoPrintClient.timelapse.delete()
OctoPrintClient.timelapse.deleteUnrendered()
OctoPrintClient.timelapse.renderUnrendered()
OctoPrintClient.timelapse.getConfig()
OctoPrintClient.timelapse.saveConfig()
OctoPrintClient.users
OctoPrintClient.users.list()
OctoPrintClient.users.get()
OctoPrintClient.users.add()
OctoPrintClient.users.update()
OctoPrintClient.users.delete()
OctoPrintClient.users.changePassword()
OctoPrintClient.users.generateApiKey()
OctoPrintClient.users.resetApiKey()
OctoPrintClient.users.getSettings()
OctoPrintClient.users.saveSettings()
OctoPrintClient.util
OctoPrintClient.wizard