> ## 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.

# Go SDK: environment variables

> Read, add, replace and remove the environment variables of a Square Cloud application with c.Apps.Envs in the Go SDK, returned as a map of strings.

Environment variables live on `c.Apps.Envs`. Every method takes `ctx` and the app id first and returns the app's variables as a `squarecloud.EnvVars` (a `map[string]string`).

Each snippet below runs on its own inside this program: paste one at a time into `main` and run `goimports` (see [Running the examples](/en/sdks/go/client#running-the-examples)).

```go theme={"system"}
package main

import (
	"context"
	"os"

	"github.com/squarecloudofc/sdk-api-go/v3"
)

var (
	ctx   = context.Background()
	c     = squarecloud.New(os.Getenv("SQUARECLOUD_API_KEY"))
	appID = "abc123def456abc123def456"
)

func main() {
	// Paste one snippet here, then run goimports.
}
```

<Note>
  Static apps have no environment variables: every method answers 400 `STATIC_APP_ENV_NOT_SUPPORTED`. A change takes effect on the next [restart](/en/sdks/go/managing_application#start-stop-and-restart).
</Note>

## Reading the variables

```go theme={"system"}
envs, err := c.Apps.Envs.Get(ctx, appID)
if err != nil {
	log.Fatal(err)
}

fmt.Println(envs["NODE_ENV"])
```

## Adding or updating variables

`Envs.Set(ctx, appID, envs)` adds the given keys and overwrites the ones that already exist. Other variables are kept. It returns every variable after the change.

```go theme={"system"}
envs, err := c.Apps.Envs.Set(ctx, appID, squarecloud.EnvVars{
	"NODE_ENV":  "production",
	"LOG_LEVEL": "info",
})
if err != nil {
	log.Fatal(err)
}

fmt.Println(envs) // every variable of the app
```

## Replacing every variable

`Envs.Replace(ctx, appID, envs)` replaces **all** variables with the given set. Keys not in the map are removed. It returns the new set.

```go theme={"system"}
if _, err := c.Apps.Envs.Replace(ctx, appID, squarecloud.EnvVars{"NODE_ENV": "production"}); err != nil {
	log.Fatal(err)
}
```

## Removing variables

`Envs.Delete(ctx, appID, keys...)` removes the given keys and returns the remaining variables. It takes the keys as variadic arguments: pass a slice with `keys...`.

```go theme={"system"}
remaining, err := c.Apps.Envs.Delete(ctx, appID, "LOG_LEVEL")
if err != nil {
	log.Fatal(err)
}

fmt.Println(remaining)
```

## Limits and errors

| Status | Code | When |
| - | - | - |
| 400 | `STATIC_APP_ENV_NOT_SUPPORTED` | The app is static |
| 400 | `INVALID_ENV_CONTENT` | The variables are not a valid object (or, for `Delete`, an empty list) |
| 400 | `TOO_MANY_ENV_VARS` | The result would have more than 256 variables |
| 400 | `ENV_NAME_TOO_LONG` | A key is over 1024 characters |
| 400 | `ENV_CONTENT_TOO_LONG` | A value is over 4096 characters |
| 400 | `READ_FAILED` | The cluster could not read the variables |

## Next steps

<CardGroup cols={2}>
  <Card title="Deploys" icon="github" href="/en/sdks/go/deploys">
    Deploy on every push, with a webhook or the GitHub App.
  </Card>

  <Card title="Environment variables API reference" icon="code" href="/en/api-reference/endpoint/apps/envs/get">
    The REST endpoints behind these methods.
  </Card>

  <Card title="Variables from the CLI" icon="terminal" href="/en/cli-reference/environment-variables">
    The same actions from the terminal.
  </Card>

  <Card title="Read variables in your code" icon="gear" href="/en/platform/environment-variables">
    Set variables from the dashboard, CLI or API and read them at runtime.
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.