You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGES.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,9 @@
2
2
3
3
## [Unreleased]
4
4
5
-
### Added
5
+
### Added
6
6
7
+
- Advice on creating custom extensions ([#944](https://github.com/stac-utils/stac-fastapi/pull/944))
7
8
- 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))
Copy file name to clipboardExpand all lines: README.md
+36-2Lines changed: 36 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ The project contains several namespace packages:
33
33
|[**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. |[](https://pypi.org/project/stac-fastapi.extensions)|
34
34
|[**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. |[](https://pypi.org/project/stac-fastapi.types)|
35
35
36
-
####Backends
36
+
## Backends
37
37
38
38
In addition to the packages in this repository, a server implemention will also require the selection of a backend to
39
39
connect with a database for STAC metadata storage. There are several different backend options, and each has their own
-[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)
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
+
classMyCustomExtension(ApiExtension):
84
+
"""Example of a custom out-of-tree extension."""
85
+
86
+
defregister(self, app):
87
+
router = APIRouter()
88
+
89
+
@router.get("/api/custom-route")
90
+
asyncdefmy_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
+
68
102
## Response Model Validation
69
103
70
104
A common question when using this package is how request and response types are validated?
0 commit comments