How to access data from the Karmen REST API with `curl`?

Hey there,

Since we still don’t have documentation for our REST API, I have decided, under the pressure of a load of questions from users, to create a simple description of how to retrieve data from our API with simple curl commands.

First of all, you need to generate a public access API token with “read all” scope. How to get one is described in my previous post: Introducing a new API token type for full read access to our REST API

To communicate with Karmen with CURL (or with any other tools or from your application), you need to add an Authorization header to your request. So a very simple HTTP GET request with curl would be as follows:

curl -k -H "Authorization: Token YOUR_TOKEN_HERE" https://backend.next.karmen.tech/api/2/users/me/

This request should give you data about your account:

{
  "id":"abcdefgh",
  "last_login":"2024-03-07T13:31:08.630545Z",
  "username":"abc@efg.com",
  "date_joined":"2022-03-23T21:28:51Z",
  ...
}

How to get a list of printers? This needs to be done with two requests. First, we’ll get a list of groups you are a member of. Then, you can call the group endpoint to list its printers.

List your groups:

curl -k -H "Authorization: Token YOUR_TOKEN_HERE" https://backend.next.karmen.tech/api/2/users/me/groups/

From the response identify your group ID:

[
  {
    "id":"123abc45",
    "name":"this is my group name",
...
  }
]

With the group ID construct your next request to list printers from the specified group identified by ID “123abc45”:

curl -k -H "Authorization: Token YOUR_TOKEN_HERE" https://backend.next.karmen.tech/api/2/groups/123abc45/printers/

In response, you get a list of your printers from the group with ID “123abc45”.

That’s it. Consuming the Karmen API is very easy. And since we are still lacking documentation (sorry for that), I’ll add some more endpoints below you can call in the same way. If you have any questions or need help, just let me know, and I’m happy to help!

We try to build our API to be self-described, so hopefully, you will see and understand how to construct your request URL easily. Here is a list of API endpoints to be used with the “read API token”.

https://backend.next.karmen.tech/api/2/files/<file_id>/videos/
https://backend.next.karmen.tech/api/2/files/<pk>/preview-images/<image_id>/
https://backend.next.karmen.tech/api/2/groups/<group_id>/file-labels/
https://backend.next.karmen.tech/api/2/groups/<group_id>/file-labels/<pk>/
https://backend.next.karmen.tech/api/2/groups/<group_id>/files/
https://backend.next.karmen.tech/api/2/groups/<group_id>/files/<pk>/
https://backend.next.karmen.tech/api/2/groups/<group_id>/print-jobs/
https://backend.next.karmen.tech/api/2/groups/<group_id>/print-queue/
https://backend.next.karmen.tech/api/2/groups/<group_id>/print-queue/<pk>/
https://backend.next.karmen.tech/api/2/groups/<group_id>/printers/
https://backend.next.karmen.tech/api/2/groups/<group_id>/users/
https://backend.next.karmen.tech/api/2/groups/<group_id>/users/<user_id>/
https://backend.next.karmen.tech/api/2/groups/<group_id>/videos/
https://backend.next.karmen.tech/api/2/groups/<group_id>/videos/<pk>/
https://backend.next.karmen.tech/api/2/printers/<pk>/
https://backend.next.karmen.tech/api/2/users/me/
https://backend.next.karmen.tech/api/2/users/me/api_keys/
https://backend.next.karmen.tech/api/2/users/me/api_keys/<pk>/
https://backend.next.karmen.tech/api/2/users/me/groups/
https://backend.next.karmen.tech/api/2/users/me/groups/<pk>/
https://backend.next.karmen.tech/api/2/groups/<id:group_id>/avatar

Martin

1 Like

Hi Martin,
I’m so glad to read this. I have found all endpoints in your API really helpful, and the setup is quick and easy. However, I couldn’t find any way to get information on whether the printer is busy or not, as well as which file is currently being printed on it. In my opinion, this is the most valuable information the API can provide to us.

Maybe I’m just overlooking something, but I haven’t found this information yet.

Thank you for your reply
Lukas

Hi Lukas,

You can access the current state of the printer and details about the print job via this endpoint:
https://preprod1.next.karmen.tech/api/2/printers/YOUR_PRINTER_ID/?fields=client.

Given that the state of the printer can change by the second, it’s advisable to query the printer at intervals of every 3 seconds for the most recent data. This aspect is somewhat complex due to various factors, including the challenges of remote communication with the printer, which can lead to latency issues and timeouts, among others.

We’ve developed a straightforward application/dashboard that displays relevant data from printers. This could serve as a starting point for you. The application is JavaScript-based, and importantly, it doesn’t store any data in the browser or on the server, ensuring you can safely input your API key - it will be forgotten once page is reloaded.
https://karmen-print-farm-dashboard.netlify.app/

You can find the source code on GitHub:

Please let me know if this is helpful.

Best,
Martin

2 Likes

Hi Martin,

thank you for your reply. The endpoint is working great for me. However, I didn’t find karmen-print-farm-dashboard as useful.

Therefore, I created my script that notifies me, when the printer has finished printing via sending a desktop notification and reading the notification loud.

You can find it on GitHub - https://github.com/Bakterio/karmen-helper. Everybody feel free to use or edit it (MIT license). I would appreciate all forks or new ideas reports.

Lukas

1 Like

Hey Lukas,

It’s nice! Thank you for sharing your code on GitHub. I haven’t tried running it locally on my computer yet, but I reviewed your code, and it seems like a really good idea!

By the way, regarding notifications in Karmen for other users: It’s possible to be notified about finished print jobs or print failures via email or Telegram — go to Settings > Notifications in Karmen Cloud.

Lukas, thank you again for sharing your code with the community!

Best, Martin

1 Like