# Python SDK (/docs/sdk.md)



<Callout type="warn" title="Not released yet">
  The ByteRun Python SDK has **not been released**. The `byterun` versions on PyPI (`0.0.x`, including the
  latest) are early previews, published only for illustration. **Do not use them yet** and do not build on
  them: commands, files and behaviour can change or disappear before the first release.

  The SDK can't run code yet. All it does today is sign this machine in and out of your ByteRun account.
</Callout>

This page shows what the preview does, so you can see where the SDK is heading. To try running code
without installing anything, use the SDK simulation in the [playground](https://byterun.io/?exp=sdk), which
needs no account.

## What the preview does [#what-the-preview-does]

* `byterun login` signs this machine in through your browser and saves a key for it.
* `byterun logout` revokes that key and deletes it from this machine.

Running code, files and SQL from Python are not available yet. Follow the
[roadmap](https://byterun.io/roadmap) for the first functional release.

## Before you start [#before-you-start]

* A ByteRun account. Sign up at [byterun.io](https://byterun.io) if you don't have one.
* Python 3.10 or newer.
* A browser on this or any other device. You approve the sign-in there.

The preview has been checked on macOS. Windows has not been checked yet.

## Sign in and out [#sign-in-and-out]

<Steps>
  <Step>
    ### Install [#install]

    Use a virtual environment so the preview stays separate from your other Python projects. This is
    optional but recommended.

    ```bash
    python3 -m venv .venv
    source .venv/bin/activate
    pip install byterun
    ```

    On Windows (PowerShell), activate with `.venv\Scripts\Activate.ps1` instead of the `source` line. This
    is the standard Python command, but `byterun` itself has not been checked on Windows yet.

    Check that it installed:

    ```bash
    byterun --version
    ```

    The `byterun` command is only available while the environment is active. Run `deactivate` to leave it,
    and the same activate line to come back.

    If you would rather not use a virtual environment, `pip install byterun` on its own works too.

    Another option is [pipx](https://pipx.pypa.io), which installs a command-line tool into its own
    environment for you. With pipx already installed, run `pipx install byterun`, and the `byterun`
    command is then available from any directory. pipx may ask you to run `pipx ensurepath` once so that
    its folder is on your `PATH`.
  </Step>

  <Step>
    ### Log in [#log-in]

    ```bash
    byterun login
    ```

    The CLI prints a short code and a link, opens the link in your browser and waits:

    ```text
    Your login code is  ABCD-EFGH
    Opening https://byterun.io/device?code=ABCD-EFGH in your browser. Check the code matches, then approve.
    Only approve if you started this login. Waiting for approval (Ctrl+C to cancel)...
    ```

    If the browser doesn't open, the CLI prints the link instead. Copy it into a browser on any device.
  </Step>

  <Step>
    ### Approve it in the browser [#approve-it-in-the-browser]

    Sign in to ByteRun if you are asked to. The page shows the same code as your terminal, the device that is
    asking, where it came from and what it will be allowed to do.

    1. Check that the code on the page **matches the code in your terminal**.
    2. Choose **Approve**. If you did not run `byterun login` yourself, choose **Deny**.

    The terminal then finishes by itself:

    ```text
    Logged in. Project starter (organization <your organization>); key expires <date>.
    ```
  </Step>

  <Step>
    ### Check it [#check-it]

    Run `byterun login` again. It says you are already logged in and does not ask for a new code.

    Your key is also listed in the app under **Settings → API keys**, named after the machine ("CLI on
    \<computer name>").
  </Step>

  <Step>
    ### Log out [#log-out]

    ```bash
    byterun logout
    ```

    This revokes the key on ByteRun's side and deletes the saved copy. Running it when you are not logged in
    does nothing and succeeds.

    Your key is saved in your home directory, not inside the virtual environment. Deleting `.venv` (or
    running `pipx uninstall byterun`) does not log you out and does not revoke the key, so run
    `byterun logout` first.
  </Step>
</Steps>

## Options [#options]

| Command                       | What it does                                                                                                                         |
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `byterun login --no-browser`  | Only prints the link. Use it over SSH or on a machine without a browser, then open the link on any device.                           |
| `byterun login --force`       | Logs in again even if this machine is already logged in. The new key is saved first, then the old one is revoked.                    |
| `byterun logout --local-only` | Deletes the saved key from this machine without telling ByteRun. The key stays valid until you revoke it in **Settings → API keys**. |

## What a login creates [#what-a-login-creates]

* **One key per machine.** Revoking one never affects another.
* **A starter project.** The key is limited to a project named `starter`, which is created for you if you
  don't have one.
* **90 days.** The key expires after about three months; run `byterun login` again then.
* **What it may do.** The approval page lists it: see your project and its files, start and stop it, run
  code and run SQL.
* **Where it is kept.** In `~/.config/byterun/credentials.json` (or under `$XDG_CONFIG_HOME`), a file only
  you can read. The key is never printed.
* **Where it works.** The SDK talks to `https://api.byterun.io`, and the key is accepted only there.

Anyone who holds the file can act as you within those limits. Log out on a machine you are giving away.

## Your plan [#your-plan]

Signing in does not need compute, so it works on any plan, including after a free trial has ended. If your
plan can't run code right now, the approval page says so and you can still approve. Running code will need
an active plan or usage left.

## Revoke a device [#revoke-a-device]

Open **Settings → API keys**, find the device and choose **Revoke**. The device stops working at once and
has to run `byterun login` again.

## If something goes wrong [#if-something-goes-wrong]

| What you see                                                           | What to do                                                                                   |
| ---------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| "the login code expired"                                               | A code lasts 10 minutes. Run `byterun login` again.                                          |
| "This code is not valid, has expired, or was already used" on the page | Run `byterun login` again and use the new code.                                              |
| "login was denied"                                                     | Someone chose **Deny** for that code. Run `byterun login` again if it was a mistake.         |
| "too many login requests" or "Too many attempts"                       | Wait a few minutes, then try again.                                                          |
| "You have reached the limit of 20 active API keys"                     | Revoke keys you no longer use in **Settings → API keys**, then approve again.                |
| "already logged in"                                                    | Nothing is wrong. Use `byterun login --force` to replace the key, or `byterun logout` first. |

Exit codes: `0` success, `1` failure (denied, expired, network), `2` usage error, `130` interrupted with
Ctrl+C.

## Links [#links]

* [GitHub repository](https://github.com/byterunio/byterun-python)
* [PyPI package](https://pypi.org/project/byterun/) (preview, see the notice at the top)
* [Roadmap](https://byterun.io/roadmap)
