No internet connection
  1. Home
  2. Technical Support

Obtaining a screenshot using the internal API

By @astigmatism
    2022-08-01 22:07:49.969Z

    I'm a student writing custom software to automate a host machine (also referred to as the "target") through the TinyPilot Voyager 2 using the exposed APIs defined in api.py (for RESTful calls) and socket_api.py (for WebSocket connections).

    For the curious and as a quick summary; the socket connection mainly handles keyboard and mouse input while the RESTful API exposes system-level operations like restart, shutdown, and status amongst others.

    I'm hoping its also possible to obtain the current state of the host video output similar to the "Actions > Screenshot" functionality in the web-client which downloads a jpeg file. Which internal API has ability exposed?

    • 6 replies
    1. Disclaimer: We don't guarantee that this method will work in future versions. For those kind of guarantees, you can sign up for a plan that includes the Enterprise REST API.

      If you check the source of the "Screenshot" button, you can see it's a simple anchor tag:

      <li class="item">
        <a id="screenshot-btn" href="/snapshot">Screenshot</a>
      </li>
      

      If you visit the /snapshot route on your device like https://tinypilot/snapshot, it will give back the image currently on the screen remote as a JPEG.

      1. A@astigmatism
          2022-08-02 04:26:10.646Z

          Ah yes, thanks for the guidance Michael!

          I'm discovering however that calling the /snapshot API results in a 500 error when I open it from a new browser tab or use a tool like Postman. Are there authentication headers or something else required on the network request?

          1. A@astigmatism
              2022-08-02 04:34:03.912Z

              Nope, my bad. I was sending the request via http and not https. Confirmed! Thanks again Michael. Loving this product :)

              1. Glad you got it working! I've filed a bug on this internally because it really should just redirect you to the HTTPS URL if you don't have plaintext HTTP enabled.

          2. J
            In reply toastigmatism:
            Jason P @jason
              2023-01-27 18:14:38.297Z

              Hey @astigmatism did you ever get a custom script working with the websocket? If so care to share the code you used? I'm starting to dive into this and am trying to connect to the internal api websocket. I see the definitions in https://github.com/tiny-pilot/tinypilot/blob/master/app/socket_api.py but am not sure if im doing wrong with my connection via websockets.

              1. It might be helpful to see how TinyPilot's web client communicates with the server via websockets:

                https://github.com/tiny-pilot/tinypilot/blob/aa0998cd0f6698abbc96b217cc0932c894122c03/app/static/js/app.js

                Or for a simplified version, you can look at how it worked in version 1.0.0 of TinyPilot:

                https://github.com/tiny-pilot/tinypilot/blob/1.0.0/app/static/js/app.js