> ## Documentation Index
> Fetch the complete documentation index at: https://ppio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 创建您的第一个 Agent 沙箱

## 注册 PPIO 账号

如果您之前没有 PPIO 账号，请通过 [https://ppio.com/user/register](https://ppio.com/user/register) 先进行注册，详情请参考 [新手指引](/support/quickstart)。

## 创建 API 密钥

通过 [PPIO API 密钥管理](https://ppio.com/settings/key-management) 页面，您可以创建 API 密钥并保存好 API 密钥的值用于后续步骤。

## 安装 SDK

您可以通过在终端执行以下命令来安装 SDK。

<CodeGroup isTerminalCommand>
  ```bash JavaScript & TypeScript SDK icon="terminal" theme={null}
  npm i ppio-sandbox dotenv
  ```

  ```bash Python SDK icon="terminal" theme={null}
  pip install ppio-sandbox python-dotenv
  ```
</CodeGroup>

## 配置环境变量

在您的项目文件夹新建 `.env` 文件（如果之前不存在的话），并配置 API 密钥。

<Warning>
  通过这种方式，对于 JavaScript 和 TypeScript 项目，需要在项目中引入 `dotenv/config` 包；对于 Python 项目，需要在项目中引入 `dotenv` 库，并调用 `load_dotenv` 方法来加载环境变量。
</Warning>

```bash .env icon="terminal" highlight={2} theme={null}
PPIO_API_KEY=sk_*** # 通过前面步骤获取的 API 密钥
```

或者您在运行代码的命令行终端中，通过设置环境变量来配置 API 密钥。

```bash Bash icon="terminal" highlight={2} theme={null}
export PPIO_API_KEY=sk_*** # 通过前面步骤获取的 API 密钥
```

## 编写代码来启动 Agent 沙箱

下面通过一个简单的实例来展示如何通过 SDK 来创建一个沙箱，并运行指定命令；

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={null}
  // index.ts
  import 'dotenv/config'
  import { Sandbox } from 'ppio-sandbox/code-interpreter'

  // The .env file should be located in the project root directory
  // dotenv/config will automatically look for .env in the current working directory
  // Or 
  // You can set the environment variable in the command line
  // export PPIO_API_KEY=sk_***

  const sandbox = await Sandbox.create()
  const execution = await sandbox.runCode('print("hello world")')
  console.log(execution.logs)

  const files = await sandbox.files.list('/tmp')
  console.log(files)

  // 不再使用时，关闭沙箱
  await sandbox.kill()
  ```

  ```python Python icon="python"  theme={null}
  # main.py
  from dotenv import load_dotenv
  from ppio_sandbox.code_interpreter import Sandbox

  # The .env file should be located in the project root directory
  # dotenv will automatically look for .env in the current working directory
  load_dotenv()

  # Or 
  # You can set the environment variable in the command line
  # export PPIO_API_KEY=sk_***

  sbx = Sandbox.create()
  execution = sbx.run_code("print('hello world')")
  print(execution.logs)

  files = sbx.files.list("/")
  print(files)

  # 不再使用时，关闭沙箱
  sbx.kill()
  ```
</CodeGroup>

执行以下命令来运行上面代码：

<CodeGroup isTerminalCommand>
  ```bash index.ts icon="terminal" theme={null}
  npx tsx ./index.ts
  ```

  ```bash main.py icon="terminal" theme={null}
  python main.py
  ```
</CodeGroup>
