# Octoprint rest API

**URL:** <https://discourse.nodered.org/t/octoprint-rest-api/10746>\
**Category:** General\
**Created:** [2 May 2019 10:27 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746 "2019-05-02T10:27:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [2 May 2019 10:27 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/1 "2019-05-02T10:27:57Z")

</div>

Did anybody made some work with octoprint rest API?

I'm monitoring some data through MQTT but having rest API is plenty f integration possibilities however I have no idea how that works and will be nice to have some flow example to start with.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [2 May 2019 12:21 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/2 "2019-05-02T12:21:08Z")

</div>

I guess should be something like this...

```auto

[{"id":"4106d02f.a1315","type":"debug","z":"4c5509ca.d3c3a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":730,"y":1480,"wires":[]},{"id":"7a4d8750.810318","type":"inject","z":"4c5509ca.d3c3a8","name":"","topic":"","payload":"/api/printer/tool?history=true&limit=2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1480,"wires":[["6cab1195.8fbfa"]]},{"id":"6cab1195.8fbfa","type":"http request","z":"4c5509ca.d3c3a8","name":"","method":"GET","ret":"obj","url":"http://192.168.1.156?api_key=C69966D056AF4ED88F48ADB119CF16B1 ","tls":"","x":480,"y":1500,"wires":[["4106d02f.a1315"]]}] 

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/07b81db75bbf57edfa7d54c67ea3b52baaa1896b.png)

However the response I get is:

```auto
msg : Object
object
_msgid: "1494f902.40e927"
topic: ""
payload: string
<html>
<head>
    <title>OctoPrint Login</title>

    <!-- le CSS -->

<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="/static/vendor/font-awesome-4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/plugin/forcelogin/static/css/forcelogin.css">

    <!-- le javascript -->

<script>
    var BASE_URL = "/";
</script>
<script src="/static/js/lib/jquery/jquery.min.js"></script>
<script src="/static/js/lib/sockjs.min.js"></script>
<script src="/static/js/lib/bootstrap/bootstrap.js"></script>
<script src="/static/js/lib/lodash.min.js"></script>

    <script type="text/javascript" src="/static/webassets/packed_client.js?4965268c"></script>

<script src="/plugin/forcelogin/static/js/main.js"></script>
</head>
<body>
    <div class="container">
        <form class="form-signin">
            <h2 class="form-signin-heading">Please log in</h2>

            <div id="login...
statusCode: 200
headers: object
content-length: "2095"
x-robots-tag: "noindex, nofollow, noimageindex"
set-cookie: array[1]
last-modified: "Wed, 30 Jan 2019 00:19:14 GMT"
etag: ""fc0f9581859fa4f96170ce4bef53bd29b18bc096""
x-from-cache: "true"
x-clacks-overhead: "GNU Terry Pratchett"
content-type: "text/html; charset=utf-8"
connection: "close"
x-node-red-request-node: "4a0648d3"
responseUrl: "http://192.168.1.156/?api_key=C69966D056AF4ED88F48ADB119CF16B1"
responseCookies: object
session_P80: object

```

As per WIKI [http://docs.octoprint.org/en/master/api/printer.html#retrieve-the-current-tool-state](http://docs.octoprint.org/en/master/api/printer.html#retrieve-the-current-tool-state)

I was expecting to get something like:

```auto
HTTP/1.1 200 OK
Content-Type: application/json

{
  "tool0": {
    "actual": 214.8821,
    "target": 220.0,
    "offset": 0
  },
  "tool1": {
    "actual": 25.3,
    "target": null,
    "offset": 0
  },
  "history": [
    {
      "time": 1395651928,
      "tool0": {
        "actual": 214.8821,
        "target": 220.0
      },
      "tool1": {
        "actual": 25.3,
        "target": null
      }
    },
    {
      "time": 1395651926,
      "tool0": {
        "actual": 212.32,
        "target": 220.0
      },
      "tool1": {
        "actual": 25.1
      }
    }
  ]
}

```

Frankly speaking I have no idea how that works and I'm trying to get some point to start with.  
Regards

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 May 2019 21:24 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/3 "2019-05-02T21:24:46Z")

</div>

Try setting the HTTP request node to GET and the URL...

`http://192.168.1.156?api_key=C69966D056AF4ED88F48ADB119CF16B1/api/printer/tool?history=true&limit=2`

_3 edits later my post says what I meant lol_

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [2 May 2019 21:30 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/4 "2019-05-02T21:30:37Z")

</div>

> [@Steve-Mcl](#):
>
> [http://192.168.1.156?api\_key=C69966D056AF4ED88F48ADB119CF16B1/api/printer/tool?history=true&limit=2](http://192.168.1.156?api_key=C69966D056AF4ED88F48ADB119CF16B1/api/printer/tool?history=true&limit=2)

same result

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 May 2019 21:32 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/5 "2019-05-02T21:32:10Z")

</div>

I made a mistake (4th one doh)

Try...

`http://192.168.1.156/api/printer/tool?history=true&limit=2&api_key=C69966D056AF4ED88F48ADB119CF16B1`

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 May 2019 21:35 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/6 "2019-05-02T21:35:18Z")

</div>

Ps I've never used this device or interface however what I can say is you are seeing a web page rather than the reply from a http end point. This should tell you the URL is formatted incorrectly or your pointing to wrong port.

Usually it's  
`http://address/path/path?parameter=value`

---

<div class="post-metadata">

**Author:** ![Jaxn](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@Jaxn](https://discourse.nodered.org/u/Jaxn)\
**Post date:** [3 May 2019 07:25 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/7 "2019-05-03T07:25:46Z")

</div>

Set the Api key as msg.header. That works for me.

```auto
[
    {
        "id": "faec932f.609f9",
        "type": "http request",
        "z": "1113cc41.f63e74",
        "name": "",
        "method": "use",
        "ret": "obj",
        "paytoqs": false,
        "url": "http://3dprinter.url/{{{command}}}",
        "tls": "",
        "proxy": "",
        "x": 550,
        "y": 180,
        "wires": [
            []
        ]
    },
    {
        "id": "d42f6979.459bd8",
        "type": "function",
        "z": "1113cc41.f63e74",
        "name": "Api",
        "func": "var stat = global.get(\"3DPRINTER\");\n\nif (msg.topic) {\n msg.topic = msg.topic.toLowerCase();\n\n msg.headers = {\n \"X-Api-Key\" : \"C5346236654563350338C516ABF\"\n };\n\n switch (msg.topic) {\n \tcase \"off\":\n\t\t msg.command = \"/api/system/commands/core/shutdown\";\n msg.method = \"POST\";\n\t\t return msg;\n\t case \"reboot\":\n \t\tmsg.command = \"/api/system/commands/core/reboot\";\n msg.method = \"POST\";\n\t\t return msg;\n\t case \"restart\":\n \t\tmsg.command = \"/api/system/commands/core/restart\";\n msg.method = \"POST\";\n\t\t return msg;\n\t case \"restart_safe\":\n \t\tmsg.command = \"/api/system/commands/core/restart_safe\";\n msg.method = \"POST\";\n\t\t return msg;\n\t case \"status\":\n \t\tmsg.command = \"/api/printer\";\n msg.method = \"GET\";\n\t\t return msg;\n\t default:\n\t\t\t return null; \n } \n}\n",
        "outputs": 1,
        "noerr": 0,
        "x": 350,
        "y": 180,
        "wires": [
            [
                "faec932f.609f9"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [5 May 2019 17:34 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/8 "2019-05-05T17:34:43Z")

</div>

> [@Jaxn](#):
>
> [{ "id": "faec932f.609f9", "type": "http request", "z": "1113cc41.f63e74", "name": "", "method": "use", "ret": "obj", "paytoqs": false, "url": "[http://3dprinter.url/{{{command}](http://3dprinter.url/%7B%7B%7Bcommand%7D)}}", "tls": "", "proxy": "", "x": 550, "y": 180, "wires": [] }, { "id": "d42f6979.459bd8", "type": "function", "z": "1113cc41.f63e74", "name": "Api", "func": "var stat = global.get("3DPRINTER");\n\nif (msg.topic) {\n msg.topic = msg.topic.toLowerCase();\n\n msg.headers = {\n "X-Api-Key" : "C5346236654563350338C516ABF"\n };\n\n switch (msg.topic) {\n \tcase "off":\n\t\t msg.command = "/api/system/commands/core/shutdown";\n msg.method = "POST";\n\t\t return msg;\n\t case "reboot":\n \t\tmsg.command = "/api/system/commands/core/reboot";\n msg.method = "POST";\n\t\t return msg;\n\t case "restart":\n \t\tmsg.command = "/api/system/commands/core/restart";\n msg.method = "POST";\n\t\t return msg;\n\t case "restart\_safe":\n \t\tmsg.command = "/api/system/commands/core/restart\_safe";\n msg.method = "POST";\n\t\t return msg;\n\t case "status":\n \t\tmsg.command = "/api/printer";\n msg.method = "GET";\n\t\t return msg;\n\t default:\n\t\t\t return null; \n } \n}\n", "outputs": 1, "noerr": 0, "x": 350, "y": 180, "wires": [["faec932f.609f9"] ] } ]

Many thanks!!!!!!!!!! That works, amazing!!!!!!

---

<div class="post-metadata">

**Author:** ![liminal](https://avatars.discourse-cdn.com/v4/letter/l/e95f7d/32.png) [@liminal](https://discourse.nodered.org/u/liminal)\
**Post date:** [31 March 2022 05:48 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/9 "2022-03-31T05:48:27Z")

</div>

Hey guys,

This is exactly what I'm trying to do, but i'm pretty new and I cant work out how to use the functions

```auto
    switch (msg.topic) {
    	case "off":
		    msg.command = "/api/system/commands/core/shutdown";
            msg.method = "POST";
		    return msg;
	    case "reboot":
    		msg.command = "/api/system/commands/core/reboot";
            msg.method = "POST";
		    return msg;
	    case "restart":
    		msg.command = "/api/system/commands/core/restart";
            msg.method = "POST";
		    return msg;
	    case "restart_safe":
    		msg.command = "/api/system/commands/core/restart_safe";
            msg.method = "POST";
		    return msg;
	    case "status":
    		msg.command = "/api/printer";
            msg.method = "GET";
		    return msg;
	    default:

```

Thanks 🙂

Its not as simple as

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/ef2a8d0166be6652eb4f2d161b0699938321a937.png)

What have I missed?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 March 2022 06:05 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/10 "2022-03-31T06:05:23Z")

</div>

Since the post you have added to is three years old, and a lot has changed since then, I think it would be better to start a new thread, explain what you are trying to do, and exactly what is not working.  
Tell us there what version of node red and nodejs you are using, and tell us if you have installed any contrib nodes.  
I will close this thread.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 March 2022 06:05 UTC](https://discourse.nodered.org/t/octoprint-rest-api/10746/11 "2022-03-31T06:05:47Z")

</div>


