Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/content/docs/environment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ and production mode otherwise.
`NODE_ENV` is a convention used in Node.js apps — the Python SDK reads
`ARCJET_ENV` only.

Python SDK users: if your config library doesn't propagate `.env` into
`os.environ` (e.g. `pydantic-settings`), pass `ARCJET_ENV` via the
`environment=` kwarg on `arcjet()` / `arcjet_sync()` — see the
<Link.Page href="/reference/python#pydantic-settings-users">Python SDK reference</Link.Page>.

In development, the timeout for connecting to Cloud API is increased.
In production, the timeout is quicker.
This is because in production your app typically runs close to an Arcjet server,
Expand Down
42 changes: 42 additions & 0 deletions src/content/docs/reference/python.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ The optional fields are:
This is useful if you are behind a load balancer or proxy that sets the client
IP address in a header. See [Load balancers &
proxies](#load-balancers--proxies) below for an example.
- `environment` (`str | None`) — Explicit development/production mode
(`"development"` or `"production"`). When `None` (default), falls back to
the
<Link.Page href="/environment#arcjet-env">`ARCJET_ENV`</Link.Page>
environment variable. Pass this when your config library doesn't propagate
`.env` into `os.environ` (e.g. `pydantic-settings`). See
Comment thread
estellajaysong marked this conversation as resolved.
[Pydantic-settings users](#pydantic-settings-users) below.

<Tabs>
<TabItem label="FastAPI (async)">
Expand Down Expand Up @@ -201,6 +208,41 @@ for more info.
The `ARCJET_KEY` environment variable is not read automatically and must be
passed explicitly via the `key` argument.

#### Pydantic-settings users

If you use
[`pydantic-settings`](https://docs.pydantic.dev/latest/concepts/pydantic_settings/),
pass
<Link.Page href="/environment#arcjet-env">`ARCJET_ENV`</Link.Page>
through the `environment=` kwarg. By design, pydantic-settings loads `.env`
into a typed `BaseSettings` object rather than writing values back to
`os.environ`, so the SDK — which reads `ARCJET_ENV` via `os.getenv` — won't
pick up the value through that channel. Without the kwarg, the SDK defaults
to production mode.

```py
from arcjet import arcjet
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env")

ARCJET_KEY: str
ARCJET_ENV: str = "development"


settings = Settings()
Comment thread
estellajaysong marked this conversation as resolved.

aj = arcjet(
key=settings.ARCJET_KEY,
rules=[...],
environment=settings.ARCJET_ENV,
)
```

`arcjet_sync()` accepts the same kwarg.

#### Load balancers & proxies

If your application is behind a load balancer, Arcjet will only see the IP
Expand Down