Python API
sugarfi (602)

Note: this package (probably) does not work on Please find it on github instead.


The API is very good, if you know JS. If you don't want to use JS, you're out
of luck. But now you can use Python too! PyRepl is a library for interacting with the API through Python. It is similar to Crosis, the JS version. Sound interesting?
Read on for a basic tutorial.


First you have to install the library. Simply clone this repo and run pip3 install -r requirements.txt,
and you'll be good to go.

The Basics

To use this library, you first need a API key. You can get one here.
Once you have that, you need a repl. Go ahead and create one. Then create file. In that file, write:

import pyrepl

Then move on to the next step.

IDs and Tokens

Each time you access a repl, you need a one-time token for it. To get that, you need an API key (see previous step)
and the repl ID. The repl ID can be retrieved in the following way:

id = pyrepl.get_json(<user>, <repl>)['id']

<user> being your username (no @) and <repl> being the name of the repl you created earlier. pyrepl.get_json
is a function that returns some useful JSON data about a repl, including the ID. Then you need a token. To get a token,

token = pyrepl.get_token(id, <key>)

<key> being your API key. You probably want to use dotenv or something similar to keep it secret.


Now that you have a token for a repl, you need to connect to it. To do that, you can use the following code:

url = pyrepl.get_url(token, '', 80)
client = pyrepl.Client(token, id, url)

First we get the URL to connect to using pyrepl.get_url. Then we create an instance of client. Now we can
move on to the final step!


Now all that's left to do is open a channel and send messages to it! For this example, we'll run the ls command.
First, to get a channel, we use:

channel ='exec', 'execer')

The first argument is the service, and the second the name of the channel. Then, we have three options for sending data:

  1. We can use, which sends the data and ignores the output. This is not what we want here,
    but it can be useful for some channels.
  2. We can use channel.get_json to get a list of dictionaries representing all the responses to the command.
    This is not what we want here, but if we wanted to examine the results in more detail, we would use this.
  3. We can use channel.get_output to get the output of the response as a string. In this case, we want the output,
    so let's use that!

This should list all the files in your repl! For complete documentation on the services and how to format messages to
them, see here.

Closing Notes

Some final notes:

  • This code is licensed under GNU GPL v 3.0. Feel free to do with it what you please, but credit would be nice!
  • This library is still in beta. I plan to improve the docs and code in the future.
  • Have any questions or comments? Feel free to comment on, open an issue on Github, or contact me on Discord.

Thanks for using PyRepl!

You are viewing a single comment. View All