Skip to content

Quickstart

Create a sandbox, write a file and run a process.

Create a sandbox, write a file to it, run a process that reads the file, and delete the sandbox.

  1. Get an API key and a project.

    You need an API key and the id of the project your sandboxes belong to. See Projects and API keys.

  2. Set your environment.

    The TypeScript and Python samples read both values from the environment.

    export SUTRA_API_KEY="your-api-key"
    export SUTRA_PROJECT="<project-id>"
  3. Install the SDK or the CLI.

    npm install @mistrive/anchor
    pip install mistrive-anchor

    Ask your Sutra contact for the anchor CLI build. Then log in once. The CLI saves the API URL, key and project in a profile.

    echo "$SUTRA_API_KEY" | anchor auth login --token-stdin --project "$SUTRA_PROJECT" \
      --api-url "https://anchor.jgcp.mistrivedev.com"
  4. Create a sandbox and run a process.

    import { AnchorClient } from "@mistrive/anchor";
    
    const client = new AnchorClient({
      baseUrl: "https://anchor.jgcp.mistrivedev.com",
      token: process.env.SUTRA_API_KEY,
      project: process.env.SUTRA_PROJECT,
    });
    
    // The service assigns the sandbox id. The lifetime stops it after 120 s.
    const sandbox = await client.create({
      sandbox: {
        maxDuration: "120s",
        resources: {
          $typeName: "anchor.types.v1.ResourceAllocation",
          vcpus: 1,
          memoryMb: BigInt(1024),
          diskMb: BigInt(1024),
        },
      },
    });
    
    // A string source is a local file path, so pass the content as a Buffer.
    await sandbox.fs.writeObject(Buffer.from("hello world\n"), "hello.txt");
    
    // Commands run in the home folder, where the file is.
    const result = await sandbox.exec({ command: "cat", args: ["hello.txt"] });
    console.log(result.stdout);
    
    await sandbox.delete();
    import os
    
    from mistrive_anchor import AnchorClient
    
    client = AnchorClient(
        base_url="https://anchor.jgcp.mistrivedev.com",
        token=os.environ["SUTRA_API_KEY"],
        project=os.environ["SUTRA_PROJECT"],
    )
    
    # The service assigns the sandbox id. The lifetime stops it after 120 s.
    sandbox = client.create(
        {
            "sandbox": {
                "maxDuration": "120s",
                "resources": {"vcpus": 1, "memoryMb": 1024, "diskMb": 1024},
            },
        }
    )
    
    # A str source is a local file path, so pass the content as bytes.
    sandbox.fs.write_object(b"hello world\n", "hello.txt")
    
    # Commands run in the home folder, where the file is.
    result = sandbox.exec({"command": "cat", "args": ["hello.txt"]})
    print(result.stdout)
    
    sandbox.delete()
    #!/usr/bin/env bash
    set -euo pipefail
    
    # Assumes you ran `anchor auth login` with a project (see the Quickstart).
    printf 'hello world\n' > hello.txt
    
    # Today the id of a deleted sandbox cannot be used again, so pick a new one.
    sandbox="hello-$(date +%s)"
    anchor sandbox create --name "$sandbox"
    anchor sandbox fs upload "$sandbox" hello.txt hello.txt
    anchor sandbox exec "$sandbox" -- cat hello.txt
    anchor sandbox delete "$sandbox"
    import { Sutra } from "@mistrive/sutra";
    
    const sutra = new Sutra({
      baseUrl: "https://anchor.jgcp.mistrivedev.com",
    });
    
    const sandbox = await sutra.sandboxes.create({
      resources: { milliCpus: 1000, memoryBytes: 1024 ** 3, diskBytes: 1024 ** 3 },
      lifecycle: { timeout: { seconds: 120, action: "DELETE" } },
    });
    
    await sandbox.files.write("hello.txt", "hello world\n");
    
    const result = await sandbox.processes.run({ argv: ["cat", "hello.txt"] });
    console.log(result.stdout);
    
    await sandbox.delete();
    from mistrive.sutra import Sutra
    
    sutra = Sutra(
        base_url="https://anchor.jgcp.mistrivedev.com",
    )
    
    sandbox = sutra.sandboxes.create(
        resources={"milli_cpus": 1000, "memory_bytes": 1024**3, "disk_bytes": 1024**3},
        lifecycle={"timeout": {"seconds": 120, "action": "DELETE"}},
    )
    
    sandbox.files.write("hello.txt", "hello world\n")
    
    result = sandbox.processes.run(argv=["cat", "hello.txt"])
    print(result.stdout)
    
    sandbox.delete()
    #!/usr/bin/env bash
    set -euo pipefail
    
    # Assumes you ran `sutra auth login` (see the Quickstart).
    sutra sandboxes create hello --timeout 120s
    echo "hello world" | sutra files write hello hello.txt
    sutra processes run hello -- cat hello.txt
    sutra sandboxes delete hello
  5. Check what happened.

    Sutra created a sandbox and gave it an id. The sample wrote hello.txt to it and ran cat in a process, which printed hello world. Then it deleted the sandbox.

    The sandbox opened no port, so nothing in it was reachable from outside. A sandbox runs until you stop or delete it, unless you give it a time limit. See Sandboxes.

Next steps

Type to search…

↑↓ navigate↵ selectEsc close