# Query Parameters

Sync component properties to URL query parameters for shareable, bookmarkable state. List the
properties to sync in `query_params`; their values are read from the URL on load and written back to
it as they change.

```python
class ProductFilter(Component):
    search: str = ""
    category: str = "all"

    query_params = ['search', 'category']
```

```interlace-demo
QueryDemo
```

## URL aliases

To expose a property under a shorter or friendlier URL key, replace the bare name with a
`{"param": ..., "as": ...}` entry. The property keeps its Python name; the URL uses the alias.

```python
query_params = [
    {"param": "search_query", "as": "q"},   # ?q=... instead of ?search_query=...
    {"param": "sort", "as": "s"},
]
```

```interlace-demo
AliasDemo
```

## Validation

Query params come from untrusted URLs - validate them on mount with a
`rules()` method (see [Validation](/docs/validation/))
and fall back to defaults if they're invalid.

```python
from django import forms

class ValidatedFilter(Component):
    price_min: int = 0
    price_max: int = 1000

    query_params = ['price_min', 'price_max']

    def rules(self):
        return {
            'price_min': forms.IntegerField(min_value=0),
            'price_max': forms.IntegerField(min_value=0, max_value=10000),
        }

    def mount(self):
        if not self.validate():
            # Invalid URL params - reset to defaults
            self.price_min = 0
            self.price_max = 1000
```
