HTTP API Example¶
This walkthrough builds an HTTP-focused Azure Functions project, customizes the generated code, adds a second endpoint, and runs everything locally.
What You Will Build¶
By the end, you will have:
- a scaffolded HTTP project
- optional OpenAPI and validation support
- two HTTP function modules (
helloandusers) - local run and curl verification flow
1) Generate the Project¶
Create a strict project with OpenAPI and validation enabled:
Move into the project and install dependencies:
Run baseline checks:
2) Understand Generated HTTP Behavior¶
With --with-validation enabled, the default hello route is generated as a
POST endpoint that validates body models.
Default route mode changes
Without validation, the default route is GET /api/hello and reads query
params. With validation enabled, it becomes POST /api/hello and expects a
JSON body.
The function_app.py entrypoint also includes OpenAPI routes when
--with-openapi is enabled:
GET /api/docsGET /api/openapi.jsonGET /api/openapi.yaml
3) Run the Function App Locally¶
In a second terminal, test the validated hello route:
curl -X POST "http://localhost:7071/api/hello" \
-H "Content-Type: application/json" \
-d '{"name":"Azure"}'
Expected response shape:
Open Swagger UI:
4) Add a New HTTP Endpoint Module¶
Use afs add to add a second endpoint scaffold:
This command:
- Creates
app/functions/users.py. - Creates
tests/test_users.py(iftests/exists). - Updates
function_app.pyimport and registration markers.
Preview before writing if needed:
5) Customize the New Endpoint¶
Edit app/functions/users.py so the route returns user list data:
from __future__ import annotations
import json
import azure.functions as func
users_blueprint = func.Blueprint() # type: ignore[no-untyped-call]
@users_blueprint.route(
route="users",
methods=["GET"],
auth_level=func.AuthLevel.ANONYMOUS,
)
def users(req: func.HttpRequest) -> func.HttpResponse:
payload = {
"items": [
{"id": 1, "name": "Ada"},
{"id": 2, "name": "Grace"},
]
}
return func.HttpResponse(
body=json.dumps(payload),
status_code=200,
mimetype="application/json",
)
Keep business logic separated
For larger endpoints, move data access and business rules into
app/services/ and keep trigger modules thin.
6) Test the New Endpoint¶
Run checks and tests:
Run locally again:
Call the new route:
Example response:
7) Common HTTP Customization Patterns¶
- Add request/response models in
app/schemas/. - Add OpenAPI annotations for each route when docs are enabled.
- Keep trigger code in
app/functions/, service logic inapp/services/. - Use pytest tests in
tests/for endpoint behavior.
Troubleshooting Notes¶
OpenAPI routes missing
Regenerate with --with-openapi, or verify your project was created with
that flag. OpenAPI routes are generated at creation time.
Validation errors on hello
With validation enabled, hello expects JSON body fields that match
app/schemas/request_models.py.
Next Steps¶
- Follow Full Stack Example for a complete strict setup.
- See Configuration for option combinations.
- Use Troubleshooting for runtime issues.