Skip to content

Commit c04177f

Browse files
authored
add advice on creating custom extensions (#944)
1 parent b021dac commit c04177f

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
## [Unreleased]
44

5-
### Added
5+
### Added
66

7+
- Advice on creating custom extensions ([#944](https://github.com/stac-utils/stac-fastapi/pull/944))
78
- Log warning when default API title, description, landing ID, or version are used to encourage proper API configuration ([#943](https://github.com/stac-utils/stac-fastapi/issues/943))
89

910
## [6.3.2] - 2026-06-27

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The project contains several namespace packages:
3333
| [**stac_fastapi.extensions**](https://github.com/stac-utils/stac-fastapi/tree/main/stac_fastapi/extensions) | Abstract base classes for [STAC API extensions](https://github.com/radiantearth/stac-api-spec/blob/master/extensions.md) and third-party extensions. | [![stac-fastapi.extensions](https://img.shields.io/pypi/v/stac-fastapi.extensions?color=%2334D058&label=pypi)](https://pypi.org/project/stac-fastapi.extensions) |
3434
| [**stac_fastapi.types**](https://github.com/stac-utils/stac-fastapi/tree/main/stac_fastapi/types) | Shared types and abstract base classes used by the library. | [![stac-fastapi.types](https://img.shields.io/pypi/v/stac-fastapi.types?color=%2334D058&label=pypi)](https://pypi.org/project/stac-fastapi.types) |
3535

36-
#### Backends
36+
## Backends
3737

3838
In addition to the packages in this repository, a server implemention will also require the selection of a backend to
3939
connect with a database for STAC metadata storage. There are several different backend options, and each has their own
@@ -51,7 +51,7 @@ Other implementations include:
5151
- [stac-fastapi-duckdb](https://github.com/Healy-Hyperspatial/stac-fastapi-duckdb): [DuckDB](https://github.com/duckdb/duckdb) (experimental)
5252
- [stac-fastapi-sqlalchemy](https://github.com/stac-utils/stac-fastapi-sqlalchemy): [PostgreSQL](https://github.com/postgres/postgres) + [PostGIS](https://github.com/postgis/postgis) via [SQLAlchemy](https://www.sqlalchemy.org/) (abandoned in favor of stac-fastapi-pgstac)
5353

54-
#### STAC-FastAPI Extensions
54+
## STAC-FastAPI Extensions
5555

5656
- [aggregation](https://github.com/stac-utils/stac-fastapi/tree/main/stac_fastapi/extensions/stac_fastapi/extensions/core/aggregation)
5757
- [bulk-transactions](https://github.com/stac-utils/stac-fastapi/blob/main/stac_fastapi/extensions/stac_fastapi/extensions/third_party/bulk_transactions.py)
@@ -65,6 +65,40 @@ Other implementations include:
6565
- [sort](https://github.com/stac-utils/stac-fastapi/tree/main/stac_fastapi/extensions/stac_fastapi/extensions/core/sort)
6666
- [transaction](https://github.com/stac-utils/stac-fastapi/tree/main/stac_fastapi/extensions/stac_fastapi/extensions/core/transaction)
6767

68+
69+
## Creating Custom Extensions
70+
71+
If you need to add deployment-specific routes (e.g., custom analytics, map tiles, or proprietary workflows) to your STAC API, you can build custom extensions without forking or modifying the core repository.
72+
73+
The framework is designed to seamlessly mount external routes alongside the standard endpoints and include them in the OpenAPI schema.
74+
75+
### 1. Define the Extension
76+
77+
Create a class that inherits from `stac_fastapi.types.extension.ApiExtension` and bind your FastAPI routes inside the `register()` method:
78+
79+
```python
80+
from fastapi import APIRouter
81+
from stac_fastapi.types.extension import ApiExtension
82+
83+
class MyCustomExtension(ApiExtension):
84+
"""Example of a custom out-of-tree extension."""
85+
86+
def register(self, app):
87+
router = APIRouter()
88+
89+
@router.get("/api/custom-route")
90+
async def my_route():
91+
return {"message": "Hello from my custom extension!"}
92+
93+
app.include_router(router)
94+
```
95+
96+
### 2. Inject it into your Backend
97+
98+
Modern `stac-fastapi` backends (such as `pgstac` and `elasticsearch-opensearch`) utilize a factory pattern (`instantiate_api`) that accepts custom extensions. Depending on the backend's implementation, you can typically inject your custom class into the application during startup via an `extra_map` or an `extensions` array.
99+
100+
For a comprehensive, real-world example of wiring routes, models, and dependencies in a standalone extension, see the [Multi-Tenant Catalogs extension](https://github.com/StacLabs/stac-fastapi-catalogs-extension).
101+
68102
## Response Model Validation
69103

70104
A common question when using this package is how request and response types are validated?

0 commit comments

Comments
 (0)