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

# Python

> The Civic Auth SDK also works with any Python backend.

The core integration points here are as follows:

* Direct users to the Civic Auth login page
* Set up an endpoint that the auth server should redirect to once complete
* Set up middleware to ensure only logged-in users can access protected parts of your app.

Use these guides to set up Civic Auth with any of the most common Python web frameworks.

<CardGroup cols={3}>
  <Card title="FastAPI" href="/integration/python/fastapi" img="https://mintcdn.com/civic/O4L8MDyrwt6gcvaS/images/auth/integration/fastapi.png?fit=max&auto=format&n=O4L8MDyrwt6gcvaS&q=85&s=e6c30984015728fdfd81b4332a661fe2" width="498" height="280" data-path="images/auth/integration/fastapi.png" />

  <Card title="Flask" href="/integration/python/flask" img="https://mintcdn.com/civic/O4L8MDyrwt6gcvaS/images/auth/integration/flask.png?fit=max&auto=format&n=O4L8MDyrwt6gcvaS&q=85&s=2593ad322226f2b6cbdccb67ca360a15" width="320" height="180" data-path="images/auth/integration/flask.png" />

  <Card title="Django" href="/integration/python/django" img="https://mintcdn.com/civic/O4L8MDyrwt6gcvaS/images/auth/integration/django.png?fit=max&auto=format&n=O4L8MDyrwt6gcvaS&q=85&s=af0776c99e0a5101937888d794562db8" width="1245" height="700" data-path="images/auth/integration/django.png" />
</CardGroup>

## Installation

Install the Civic Auth Python SDK using pip:

```bash theme={null}
pip install civic-auth
```

For framework-specific integrations:

<CodeGroup>
  ```bash FastAPI theme={null}
  pip install "civic-auth[fastapi]"
  ```

  ```bash Flask theme={null}
  pip install "civic-auth[flask]"
  ```

  ```bash Django theme={null}
  pip install "civic-auth[django]"
  ```
</CodeGroup>

If you're using the `uv` package manager:

<CodeGroup>
  ```bash FastAPI theme={null}
  uv add "civic-auth[fastapi]"
  ```

  ```bash Flask theme={null}
  uv add "civic-auth[flask]"
  ```

  ```bash Django theme={null}
  uv add "civic-auth[django]"
  ```
</CodeGroup>

## Usage

The Civic Auth Python SDK provides a flexible API that works with any Python web framework. For framework-specific integrations, see the guides above.

### Getting User Information on the Backend

Here are some examples of using the get\_user function in popular Python server environments. Note - this snippet assumes you have followed the steps to integrate login with your app as described [here](/integration/python).

<CodeGroup>
  ```fastapi FastAPI theme={null}
  from fastapi import Depends
  from civic_auth.integrations.fastapi import create_auth_dependencies, create_auth_router

  civic_auth_dep, get_current_user, require_auth = create_auth_dependencies(config)

  @app.get("/admin/hello", dependencies=[Depends(require_auth)])
  async def hello(user = Depends(get_current_user)):
      return f"hello {user.name}!"
  ```

  ```flask Flask theme={null}
  from civic_auth.integrations.flask import get_civic_user, civic_auth_required

  @app.route("/admin/hello")
  @civic_auth_required
  async def hello():
      user = await get_civic_user()
      return f"hello {user.name}!"
  ```

  ```django Django theme={null}
  from civic_auth.integrations.django import civic_auth_required

  @civic_auth_required
  def hello(request):
      user = request.civic_user
      return HttpResponse(f"hello {user.name}!")
  ```
</CodeGroup>

## Getting the Access Token

Use CivicAuth-managed storage to retrieve tokens. Prefer the SDK helper that exposes a `CivicAuth` instance and call `await civic.get_tokens()` (auto-refreshes when needed). If your endpoint receives a bearer token from a frontend, you can also read it from the `Authorization` header.

```python theme={null}
# Preferred: via CivicAuth instance
tokens = await civic.get_tokens()  # { 'access_token': '...', 'id_token': '...', ... }
access_token = tokens["access_token"]

# Fallback: read bearer token from headers (example)
auth = request.headers.get("Authorization", "")
access_token = auth.removeprefix("Bearer ").strip()
```

<Info>
  Need structured validation? See the Pydantic AI recipe: [/civic/recipes/pydantic-ai](/civic/recipes/pydantic-ai).
</Info>
