From 48ed547e3598358d5c84f2d4821eb2378436dd0e Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:24:30 -0700 Subject: [PATCH 1/8] docs(website): start the Python guide from the FastAPI template repository Replace the cg init setup with Use this template and git clone, add the template's make ci check and curl requests, and match the project tree to the template. --- .../src/content/docs/guides/using-python.mdx | 60 ++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/website/src/content/docs/guides/using-python.mdx b/website/src/content/docs/guides/using-python.mdx index 311ed82eb..64cb3ddb8 100644 --- a/website/src/content/docs/guides/using-python.mdx +++ b/website/src/content/docs/guides/using-python.mdx @@ -27,23 +27,15 @@ This guide walks you through setting up a new project from the CommonGrants Fast Get a FastAPI project up and running with the following steps: -1. Create a new directory for your project: +1. Open the [FastAPI template repository](https://github.com/common-grants/cg-api-template-fastapi) on GitHub and click **Use this template** to create your own repository from it. - ```bash - mkdir common-grants-api - cd common-grants-api - ``` - -2. Set up your project using the CommonGrants CLI, accepting the default answers at the prompts: +2. Clone your new repository: ```bash - npx @common-grants/cli@0.4.1 init --template fast-api + git clone https://github.com//.git + cd ``` - `npx` ships with Node.js. Bun users can run `bunx` instead. `pnpm dlx` can't run `cg init`. - - The command also leaves `main.tsp`, `tspconfig.yaml`, `package.json`, `package-lock.json`, and `node_modules/` behind. The Python project doesn't use them, so you can delete them. - 3. Install the dependencies: ```bash @@ -52,13 +44,38 @@ Get a FastAPI project up and running with the following steps: If Poetry picks an older interpreter, run `poetry env use python3.11` first. -4. Run the project: +4. Check that everything works before you change anything: + + ```bash + make ci + ``` + + This runs the format, lint, and type checks, then the test suite with coverage. + +5. Run the project: ```bash make dev ``` -5. Open the API docs at `http://localhost:8000/docs`. Press `Ctrl+C` in the terminal to stop the server when you're done. +6. Open the API docs at `http://localhost:8000/docs` to try a request from the browser. + +7. Or, in another terminal, make a few requests with `curl`: + + ```bash + # A page of opportunities, most recently modified first + curl 'http://localhost:8000/common-grants/opportunities?pageSize=2' + + # Filtered and sorted search + curl -X POST http://localhost:8000/common-grants/opportunities/search \ + -H 'Content-Type: application/json' \ + -d '{"filters":{"status":{"operator":"in","value":["open"]}},"sorting":{"sortBy":"title","sortOrder":"asc"}}' + + # The OpenAPI document, generated from the SDK's models + curl http://localhost:8000/openapi.json + ``` + + Press `Ctrl+C` in the server's terminal to stop it when you're done. @@ -70,22 +87,21 @@ The template includes the following files and directories: - Makefile # Development workflow commands - README.md # Project documentation +- PORTING.md # Walkthrough for connecting your own data - pyproject.toml # Python project configuration and dependencies -- poetry.lock # Locked versions of dependencies (created by make install) +- poetry.lock # Locked versions of dependencies - .coveragerc # Test coverage configuration - .gitignore # Files Git ignores - src/ - common_grants/ - - api.py # FastAPI application setup and config + - api.py # FastAPI application setup; choose the data source here - constants.py # Set ORGANIZATION_DOMAIN to your domain for stable opportunity IDs + - data/ # Bundled sample opportunities - routes/ # API route handlers - - schemas/ # Re-exports the SDK's Pydantic models + - schemas/ # The opportunity model the API serves - scripts/ # OpenAPI generator - - services/ # Business logic and data operations -- tests/ - - conftest.py # Shared test fixtures - - common_grants/ - - routes/ # Route-related tests + - services/ # The data source interface and its sample-data implementation +- tests/ # Test suites for the app, routes, sample data, and OpenAPI document From 631bc88b6fa283215a89d3fbe8c852c00dbcd5ac Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:24:31 -0700 Subject: [PATCH 2/8] docs(website): correct the Python guide's file and class names for the template The template has no schemas re-export, OpportunityService, or plugin.py. Point to opportunity.py, the OpportunityRepository seam and create_app(), note that routes already omit unset fields, and drop the Pyright workaround the template's Opportunity alias makes unnecessary. --- .../src/content/docs/guides/using-python.mdx | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/website/src/content/docs/guides/using-python.mdx b/website/src/content/docs/guides/using-python.mdx index 64cb3ddb8..f171c1c81 100644 --- a/website/src/content/docs/guides/using-python.mdx +++ b/website/src/content/docs/guides/using-python.mdx @@ -149,7 +149,7 @@ The check should report `Spec is compliant with base spec, with 3 warnings`. The ### Validate data with the SDK -The template's `src/common_grants/schemas/__init__.py` re-exports the SDK's models, so the routes and services already use them. Validate records from your data source the same way: +The template's routes and services already use the SDK's models, and `src/common_grants/schemas/opportunity.py` defines the opportunity model the API serves as the SDK's `OpportunityBase`. Validate records from your data source the same way: ```python from common_grants_sdk.schemas.pydantic import OpportunityBase @@ -161,7 +161,7 @@ opportunity = OpportunityBase.model_validate(raw_data) ### Implementing services -The services layer in `src/common_grants/services/` holds the business logic and data operations for the API. Focus on `opportunity.py`, which implements the `OpportunityService` class behind these routes: +The services layer in `src/common_grants/services/` holds the data operations for the API. `repository.py` defines the `OpportunityRepository` interface behind these routes, and `fixtures.py` implements it over the bundled sample data: - `GET /common-grants/opportunities` - `GET /common-grants/opportunities/{oppId}` @@ -169,9 +169,10 @@ The services layer in `src/common_grants/services/` holds the business logic and Specific changes to make: -- Replace the mock data with a real data source, e.g. a database query or a remote API call, and validate each record with an SDK model. -- Add the sorting and filtering logic to the `OpportunityService.search_opportunities` method. -- Add `response_model_exclude_unset=True` to each route decorator in `src/common_grants/routes/opportunities.py`, so optional fields you don't set are omitted instead of sent as `null`, which the protocol rejects. +- Implement the interface's `list`, `get`, and `search` methods against a real data source, e.g. a database query or a remote API call, including the sorting and filtering in `search`, and validate each record with an SDK model. +- Pass your implementation to `create_app()` in `src/common_grants/api.py` in place of `fixture_repository`. + +The template's routes already omit optional fields you don't set instead of sending them as `null`. Its [`PORTING.md`](https://github.com/common-grants/cg-api-template-fastapi/blob/main/PORTING.md) walks through stable IDs, date and money handling, and which tests to keep. ### Adding custom fields @@ -181,13 +182,11 @@ For example, let's say you need to add a `legacyId` field to map opportunities t #### Define a plugin -Create a plugin that declares the custom field. The plugin registers an `Opportunity` schema with a typed `custom_fields` property: +Create a plugin that declares the custom field. The plugin registers an `Opportunity` schema with a typed `custom_fields` property. Replace the contents of the template's opportunity model file with it, since the routes, response models, and services all read `Opportunity` from there: -```python title=src/common_grants/schemas/plugin.py +```python title=src/common_grants/schemas/opportunity.py """Custom fields for this API's data source.""" -from typing import Optional - from common_grants_sdk.extensions import ( CustomField, CustomFieldSet, @@ -203,7 +202,7 @@ from pydantic import Field class OppFields(CustomFieldSet): """Custom fields on the Opportunity schema.""" - legacy_id: Optional[CustomField[int]] = Field( + legacy_id: CustomField[int] | None = Field( default=None, description="Maps to the opportunity_id in the legacy system", ) @@ -225,7 +224,7 @@ Opportunity = my_plugin.schemas.Opportunity.common_schema Validate your data with the plugin's schema instead of the base model, for example in a service. The custom field is validated and fully typed: ```python -from common_grants.schemas.plugin import Opportunity +from common_grants.schemas.opportunity import Opportunity opportunity = Opportunity.model_validate({ "id": "573525f2-8e15-4405-83fb-e6523511d893", @@ -244,7 +243,7 @@ if fields and fields.legacy_id: fields.legacy_id.value # int ``` -At runtime `Opportunity` is a subclass of `OpportunityBase`, so the template's response models accept it. Pyright treats the custom-fields type parameter as invariant, though. Once the service returns `Opportunity` records, change its `OpportunityBase` annotations to `Opportunity`, and build the list and search responses with `Paginated[Opportunity]` and `Filtered[Opportunity, dict]` instead of `OpportunitiesListResponse` and `OpportunitiesSearchResponse`, or `make check-types` fails. Custom fields the plugin doesn't declare are kept under their wire keys in `custom_fields.model_extra`. +The template's response models and services are built on `Opportunity`, so they serve the custom field and `make check-types` still passes. Custom fields the plugin doesn't declare are kept under their wire keys in `custom_fields.model_extra`. For a one-off extension without a plugin, use `OpportunityBase.with_custom_fields()`. See the [extensions guide](https://github.com/HHS/simpler-grants-protocol/blob/main/lib/python-sdk/common_grants_sdk/extensions/README.md) for custom filters, transforms, and publishing plugins. From 24199547983ecbfa842109e534a27df6a528037f Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:24:31 -0700 Subject: [PATCH 3/8] docs(website): reframe the SDK upgrade as upgrading from an earlier SDK The template now depends on SDK 0.9, so the upgrade applies only to projects from an earlier template. Move it after the getting-started flow, use the ^0.9.0 range, and name the template's real camelCase arguments. --- .../src/content/docs/guides/using-python.mdx | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/website/src/content/docs/guides/using-python.mdx b/website/src/content/docs/guides/using-python.mdx index f171c1c81..c1bb5a6e7 100644 --- a/website/src/content/docs/guides/using-python.mdx +++ b/website/src/content/docs/guides/using-python.mdx @@ -11,7 +11,7 @@ Learn how to implement a CommonGrants API in Python using the [FastAPI](https:// ## Quickstart -This guide walks you through setting up a new project from the CommonGrants FastAPI template, then upgrading the SDK to validate your data and define custom fields. +This guide walks you through setting up a new project from the CommonGrants FastAPI template, then using the SDK to validate your data and define custom fields. :::note[Prerequisites] @@ -107,32 +107,7 @@ The template includes the following files and directories: ## Next steps -Once the template is running, upgrade the SDK and connect the API to your data. - -### Upgrade the SDK - -The [`common-grants-sdk`](https://pypi.org/project/common-grants-sdk/) package provides Pydantic models, an extension framework, and an HTTP client that match protocol v0.5. The template pins an older release, so upgrade it: - -```bash -poetry add common-grants-sdk@0.9.0 -``` - -The SDK is split into modules, each with its own import path: - -| Import path | Description | -| ------------------------------------ | ----------------------------------------------------------------- | -| `common_grants_sdk.schemas.pydantic` | Pydantic models for opportunities, awards, filters, and responses | -| `common_grants_sdk.extensions` | Custom field and plugin framework | -| `common_grants_sdk.client` | HTTP client with auth, pagination, and search | - -The SDK also ships the v0.5 award models, award filters, and a `client.awards` resource, which follow the same patterns as their opportunity counterparts. The template doesn't implement the award routes, which are optional in v0.5. - -SDK 0.9 types model constructors with snake_case field names, and the template passes camelCase keyword arguments. The app and tests still run, but `make check-types` reports errors until you rename the arguments in `src/common_grants/services/opportunity.py`, `src/common_grants/services/utils.py`, and `tests/conftest.py`: for example, `pageSize=page_size` becomes `page_size=page_size`. Leave dictionary keys such as `"pageSize"` alone, since they're the wire format. Then confirm the project type-checks and passes its tests: - -```bash -make check-types -make test -``` +Once the template is running, validate your spec and connect the API to your data. ### Validate your spec @@ -247,6 +222,31 @@ The template's response models and services are built on `Opportunity`, so they For a one-off extension without a plugin, use `OpportunityBase.with_custom_fields()`. See the [extensions guide](https://github.com/HHS/simpler-grants-protocol/blob/main/lib/python-sdk/common_grants_sdk/extensions/README.md) for custom filters, transforms, and publishing plugins. +## Upgrading from an earlier SDK + +The [`common-grants-sdk`](https://pypi.org/project/common-grants-sdk/) package provides Pydantic models, an extension framework, and an HTTP client that match protocol v0.5. The template depends on SDK 0.9. If your project started from an earlier version of the template, which depended on SDK 0.8, upgrade it: + +```bash +poetry add common-grants-sdk@^0.9.0 +``` + +The SDK is split into modules, each with its own import path: + +| Import path | Description | +| ------------------------------------ | ----------------------------------------------------------------- | +| `common_grants_sdk.schemas.pydantic` | Pydantic models for opportunities, awards, filters, and responses | +| `common_grants_sdk.extensions` | Custom field and plugin framework | +| `common_grants_sdk.client` | HTTP client with auth, pagination, and search | + +The SDK also ships the v0.5 award models, award filters, and a `client.awards` resource, which follow the same patterns as their opportunity counterparts. The template doesn't implement the award routes, which are optional in v0.5. + +SDK 0.9 types model constructors with snake_case field names, and earlier versions of the template pass camelCase keyword arguments. The app and tests still run, but `make check-types` reports errors until you rename the arguments. In the template, `SearchSorting(sortBy=...)` in `src/common_grants/routes/opportunities.py` becomes `SearchSorting(sort_by=...)`, and `PaginatedBodyParams(pageSize=...)` in `tests/test_fixtures.py` becomes `PaginatedBodyParams(page_size=...)`. Leave `sortOrder=` alone, since the template's `SearchSorting` declares that field with its camelCase alias, and leave dictionary keys such as `"pageSize"` alone, since they're the wire format. Then confirm the project type-checks and passes its tests: + +```bash +make check-types +make test +``` + ## Additional resources - [CommonGrants Python SDK](https://github.com/HHS/simpler-grants-protocol/tree/main/lib/python-sdk#readme) From ef9c201622d76caf2a1f7c8668e378050e39f2c6 Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:24:31 -0700 Subject: [PATCH 4/8] docs(website): show the Python guide's spec check for pnpm, npm, and bun Put cg check spec in synchronized package-manager tabs and drop make check-spec, which the template does not have. --- .../src/content/docs/guides/using-python.mdx | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/website/src/content/docs/guides/using-python.mdx b/website/src/content/docs/guides/using-python.mdx index c1bb5a6e7..937c9a636 100644 --- a/website/src/content/docs/guides/using-python.mdx +++ b/website/src/content/docs/guides/using-python.mdx @@ -5,7 +5,7 @@ sidebar: order: 0 --- -import { FileTree, Steps } from "@astrojs/starlight/components"; +import { FileTree, Steps, TabItem, Tabs } from "@astrojs/starlight/components"; Learn how to implement a CommonGrants API in Python using the [FastAPI](https://fastapi.tiangolo.com/) web framework and the [CommonGrants Python SDK](https://github.com/HHS/simpler-grants-protocol/tree/main/lib/python-sdk#readme). @@ -17,7 +17,7 @@ This guide walks you through setting up a new project from the CommonGrants Fast - [Python](https://www.python.org/downloads/) 3.11 or later (check with `python3 --version`). - [Poetry](https://python-poetry.org/docs/#installation) 2 or later (check with `poetry --version`). The template's `Makefile` runs every command through Poetry. -- [Node.js](https://nodejs.org/) v22 or later (check with `node --version`). `npx` runs the CommonGrants CLI, which scaffolds the project and validates your spec. +- [Node.js](https://nodejs.org/) v22 or later (check with `node --version`), with pnpm, npm, or bun to run the CommonGrants CLI, which validates your spec. - Port `8000` free for the development server. ::: @@ -115,10 +115,33 @@ Generate the OpenAPI spec from the FastAPI app and check it against the CommonGr ```bash make gen-openapi +``` + + + + +```bash +pnpm dlx @common-grants/cli@0.5.0 check spec openapi.yaml --protocol-version 0.5.0 +``` + + + + +```bash npx @common-grants/cli@0.5.0 check spec openapi.yaml --protocol-version 0.5.0 ``` -The check should report `Spec is compliant with base spec, with 3 warnings`. The warnings list the award routes, which are optional in v0.5 and not implemented by the template. Errors, such as a missing or extra route, fail the command with exit code 1. `make check-spec` runs the same check with a globally installed `cg` 0.5.0 or later. + + + +```bash +bunx @common-grants/cli@0.5.0 check spec openapi.yaml --protocol-version 0.5.0 +``` + + + + +The check should report `Spec is compliant with base spec, with 3 warnings`. The warnings list the award routes, which are optional in v0.5 and not implemented by the template. Errors, such as a missing or extra route, fail the command with exit code 1. `cg check spec` validates the API specification, not the running server. To check the code, run `make test` and `make dev`. Press `Ctrl+C` to stop the server. From 22fc59232dd8c8e4fb5ba2d9a3985efaad4963f8 Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:24:32 -0700 Subject: [PATCH 5/8] docs(website): title the Python guide Using Python --- website/src/content/docs/guides/using-python.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/content/docs/guides/using-python.mdx b/website/src/content/docs/guides/using-python.mdx index 937c9a636..e5fb70535 100644 --- a/website/src/content/docs/guides/using-python.mdx +++ b/website/src/content/docs/guides/using-python.mdx @@ -1,5 +1,5 @@ --- -title: Building a Python API +title: Using Python description: Learn how to implement a CommonGrants API in Python. sidebar: order: 0 From 131429b6ee15803a54ec83942e8d6820112989bb Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:24:32 -0700 Subject: [PATCH 6/8] docs(cli): drop the Python guide from the cg init reference checklist --- lib/cli/DEVELOPMENT.md | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/cli/DEVELOPMENT.md b/lib/cli/DEVELOPMENT.md index b9f756c54..3d826af8b 100644 --- a/lib/cli/DEVELOPMENT.md +++ b/lib/cli/DEVELOPMENT.md @@ -75,7 +75,6 @@ Steps to follow when preparing a new release of the CommonGrants CLI. Feature wo - [ ] [Fast API template README](../../templates/fast-api/README.md) - [ ] [Express.js template README](../../templates/express-js/README.md) - [ ] [Quickstart guide](../../website/src/content/docs/getting-started.mdx) - - [ ] [Using Python](../../website/src/content/docs/guides/using-python.mdx) - [ ] [Using TypeScript](../../website/src/content/docs/guides/using-typescript.mdx) - [ ] If the `check spec` command was updated, references to that command in the following locations have been updated: - [ ] [FastAPI template Makefile](../../templates/fast-api/Makefile) From 04b8df89b44c591d386ecadf9151513d61acbe62 Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:50:44 -0700 Subject: [PATCH 7/8] docs(website): clarify that bun runs the CLI without Node.js --- website/src/content/docs/guides/using-python.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/content/docs/guides/using-python.mdx b/website/src/content/docs/guides/using-python.mdx index e5fb70535..132378a4c 100644 --- a/website/src/content/docs/guides/using-python.mdx +++ b/website/src/content/docs/guides/using-python.mdx @@ -17,7 +17,7 @@ This guide walks you through setting up a new project from the CommonGrants Fast - [Python](https://www.python.org/downloads/) 3.11 or later (check with `python3 --version`). - [Poetry](https://python-poetry.org/docs/#installation) 2 or later (check with `poetry --version`). The template's `Makefile` runs every command through Poetry. -- [Node.js](https://nodejs.org/) v22 or later (check with `node --version`), with pnpm, npm, or bun to run the CommonGrants CLI, which validates your spec. +- To run the CommonGrants CLI, which validates your spec: [Node.js](https://nodejs.org/) v22 or later (check with `node --version`) with pnpm or npm, or bun. - Port `8000` free for the development server. ::: From a8cf95fcc4072281b846dbb633f8de146241afd2 Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Wed, 28 Oct 2026 09:31:14 -0700 Subject: [PATCH 8/8] docs(cli): add the Python guide to the check spec checklist --- lib/cli/DEVELOPMENT.md | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cli/DEVELOPMENT.md b/lib/cli/DEVELOPMENT.md index 3d826af8b..78acc9a6e 100644 --- a/lib/cli/DEVELOPMENT.md +++ b/lib/cli/DEVELOPMENT.md @@ -78,3 +78,4 @@ Steps to follow when preparing a new release of the CommonGrants CLI. Feature wo - [ ] [Using TypeScript](../../website/src/content/docs/guides/using-typescript.mdx) - [ ] If the `check spec` command was updated, references to that command in the following locations have been updated: - [ ] [FastAPI template Makefile](../../templates/fast-api/Makefile) + - [ ] [Using Python](../../website/src/content/docs/guides/using-python.mdx)