Runtime Environment
Inject secrets into sandboxes, functions, and scaling groups without exposing them in source or images.
Secrets let you inject credentials (API keys, database URLs, tokens) into a
workload without putting them in source code or baking them into an image.
Pass a list of Secret references to the secrets parameter of Sandbox,
ScalingGroup, or @chalkcompute.function; each one is resolved at deploy
time and exposed to the workload as an environment variable.
import chalkcompute
from chalkcompute import Secret
@chalkcompute.function(
secrets=[
Secret.from_chalk_env("OPENAI_API_KEY"),
Secret.from_chalk_integration("prod_postgres"),
],
)
def call_api(prompt: str) -> str:
import os
openai_api_key = os.environ["OPENAI_API_KEY"]
database_url = os.environ["DATABASE_URL"]
...
The same secrets=[...] parameter and the same Secret constructors work
identically across all three primitives.
The secrets parameter exposes real values as environment variables readable
by workload code. Use Sandbox.add_credentials(hosts, *credentials) to let untrusted sandbox code
authenticate to external APIs while keeping the real credentials outside the
sandbox. Call it before .run().
Store the API key as a Chalk secret, then pass its name
to add_credentials:
chalk secret set OPENAI_API_KEY < /path/to/openai-api-key
import chalkcompute
sandbox = (
chalkcompute.Sandbox(
image=chalkcompute.Image.debian_slim("3.12"),
network_policy=chalkcompute.NetworkPolicy(
allowed_hosts=["api.openai.com"],
),
)
.add_credentials("api.openai.com", "OPENAI_API_KEY")
.run()
)
try:
result = sandbox.exec("python", "-c", """
import os
import urllib.request
request = urllib.request.Request(
"https://api.openai.com/v1/models",
headers={"Authorization": f"Bearer {os.environ['OPENAI_API_KEY']}"},
)
with urllib.request.urlopen(request, timeout=30) as response:
print(response.status)
""")
print(result.stdout_text)
finally:
sandbox.terminate()
Inside the sandbox, OPENAI_API_KEY contains a placeholder. The sandbox’s
egress proxy replaces that placeholder with the real secret when it appears
in request headers sent to api.openai.com. Requests to other hosts do not
receive this credential, and substitution does not apply to URLs or request
bodies. The example’s network policy also blocks requests to other hosts.
The hosts argument accepts a hostname or a list of host patterns, using the
same matching rules as allowed_hosts. Each credential can be an existing
Chalk secret name, a Secret.from_chalk_env(...) reference, or a
Secret.from_local_env(...) reference. By default, the placeholder environment
variable has the secret’s name. Call add_credentials again to give another
host its own credentials.
Keep these credentials out of the sandbox’s env and secrets parameters,
which expose their values to sandbox code. Credential injection requires the
host compute class.
For a complete agent example, see Keep the real key out of the sandbox. For custom matching and header rewriting, see per-host request rules.
| Constructor | Description |
|---|---|
Secret.from_chalk_env(name, *, alias=None, prefix=None) | Reference a standalone secret by name from Chalk’s managed secret store, e.g. Secret.from_chalk_env("OPENAI_API_TOKEN"). |
Secret.from_chalk_integration(name, *, keys=None, aliases=None, prefix=None) | Inject every secret associated with a named Chalk integration (e.g. "prod_postgres") as env vars. |
Secret.from_local_env(local_env_var, *, env_var_name=None) | Read a local environment variable at deploy time and upsert it as a Chalk secret, handy for dev loops. |
Secret.from_local_env_file(path) | Upsert every KEY=VALUE line of a local .env file as a Chalk secret. |
Secret.from_env and Secret.from_integration still work as deprecated
aliases for from_chalk_env/from_chalk_integration, but new code should
use the from_chalk_* names.
By default, a secret is injected under an environment variable matching its
Chalk secret name. Use alias to rename it, or prefix to namespace every
key an integration exposes:
Secret.from_chalk_env("OPENAI_API_TOKEN", alias="API_KEY")
# injects OPENAI_API_TOKEN's value as API_KEY
Secret.from_chalk_integration("prod_postgres", prefix="PG_")
# injects prod_postgres's keys as PG_HOST, PG_USER, PG_PASSWORD, etc.
Secret.from_chalk_integration("prod_postgres", keys=["host", "password"])
# injects only the listed keys, instead of every key the integration exposes
Secret.from_local_env and Secret.from_local_env_file resolve a value from
your own machine at deploy time, upsert it as a Chalk secret, and inject it
like any other secret, which is useful for iterating locally without first
creating the secret through chalk secret set or the dashboard:
from chalkcompute import Secret
Secret.from_local_env("OPENAI_API_KEY")
Secret.from_local_env("OPENAI_API_KEY", env_var_name="API_KEY") # inject under a different name
Secret.from_local_env_file(".env")
These are lazy references — resolved once, when the workload actually deploys — not evaluated at import time.