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

# 環境変数

> client.apps.envs でアプリケーションの環境変数を読み取り、追加、置き換え、削除します。

環境変数は `client.apps.envs` にあります。すべてのメソッドは第 1 引数にアプリ ID を取り、アプリの変数をプレーンな `dict[str, str]` として返します。

<Note>
  静的アプリには環境変数がありません。すべてのメソッドが 400 `STATIC_APP_ENV_NOT_SUPPORTED` を返します。変更は次回の[再起動](/ja/sdks/py/managing_application)で反映されます。
</Note>

## 変数の読み取り

```python theme={"system"}
envs = client.apps.envs.get(app_id)

print(envs.get("NODE_ENV"))
```

## 変数の追加または更新

`envs.set(app_id, envs)` は、指定したキーを追加し、既存のキーは上書きします。その他の変数は維持されます。変更後のすべての変数を返します。

```python theme={"system"}
envs = client.apps.envs.set(app_id, {
    "NODE_ENV": "production",
    "LOG_LEVEL": "info",
})
```

## すべての変数の置き換え

`envs.replace(app_id, envs)` は、**すべての**変数を指定したセットで置き換えます。dict に含まれないキーは削除されます。`{}` を渡すとすべて消去されます。

```python theme={"system"}
client.apps.envs.replace(app_id, {"NODE_ENV": "production"})
```

## 変数の削除

`envs.delete(app_id, keys)` は指定したキーを削除し、残りの変数を返します。`keys` はリスト (文字列の任意のイテラブル) または単一のキーです。

```python theme={"system"}
remaining = client.apps.envs.delete(app_id, ["LOG_LEVEL"])

# A single key also works
remaining = client.apps.envs.delete(app_id, "LOG_LEVEL")
```

## 制限とエラー

| ステータス | コード                            | 発生条件                                  |
| ----- | ------------------------------ | ------------------------------------- |
| 400   | `STATIC_APP_ENV_NOT_SUPPORTED` | アプリが静的アプリである                          |
| 400   | `INVALID_ENV_CONTENT`          | 変数が有効なオブジェクトではない (`delete` の場合は空のリスト) |
| 400   | `TOO_MANY_ENV_VARS`            | 結果が 256 個を超える変数になる                    |
| 400   | `ENV_NAME_TOO_LONG`            | キーが 1024 文字を超えている                     |
| 400   | `ENV_CONTENT_TOO_LONG`         | 値が 4096 文字を超えている                      |
| 400   | `READ_FAILED`                  | クラスターが変数を読み取れなかった                     |
