Skip to main content

FastAPI-XML

tests codecov license versions

Seamless XML support for FastAPI — parse XML request bodies and return XML responses using standard Python dataclasses, with full OpenAPI integration.

pip install fastapi-xml

Built on xsdata for XML serialization, it slots into FastAPI the same way JSON does.

Features

  • XML request bodies — replace Body() with XmlBody(), your endpoint receives a populated dataclass
  • XML responses — return any dataclass and get back application/xml (or text/xml)
  • OpenAPI schema — Swagger UI renders XML examples and schemas automatically
  • Zero learning curve — works like FastAPI's JSON, just swap in XmlRoute and XmlBody
  • Two media types — XmlAppResponse (application/xml) and XmlTextResponse (text/xml)

Quick Start

from dataclasses import dataclass, field
from fastapi import FastAPI
from fastapi_xml import XmlRoute, XmlAppResponse, XmlBody, add_openapi_extension

@dataclass
class HelloWorld:
    message: str = field(metadata={"type": "Element"})

app = FastAPI(title="FastAPI-XML", default_response_class=XmlAppResponse)
app.router.route_class = XmlRoute
add_openapi_extension(app)

@app.post("/echo", response_model=HelloWorld)
def echo(x: HelloWorld = XmlBody()) -> HelloWorld:
    x.message += " Forever!"
    return x
<!-- Request -->
<HelloWorld><message>Hello</message></HelloWorld>

<!-- Response -->
<HelloWorld><message>Hello Forever!</message></HelloWorld>

How It Works

XmlRoute replaces FastAPI's default APIRoute. When a request arrives with an XML body, the XmlDecoder (wrapping xsdata's XmlParser) deserializes it into your dataclass. On the way out, XmlResponse serializes the return value back to XML via xsdata's XmlSerializer.

Response Types

Class Content-Type
XmlAppResponse application/xml
XmlTextResponse text/xml

OpenAPI

Call add_openapi_extension(app) to generate accurate XML schemas in your OpenAPI spec. The Swagger UI at /docs will show XML examples and let you test endpoints directly.

Example

from dataclasses import dataclass, field
from fastapi import FastAPI
from fastapi_xml import XmlRoute, XmlAppResponse, XmlBody


@dataclass
class Item:
    name: str = field(metadata={"type": "Element"})
    price: float = field(metadata={"type": "Element"})

@dataclass
class Order:
    id: str = field(metadata={"type": "Element"})
    items: list[Item] = field(metadata={"type": "Element", "name": "Item"})


app = FastAPI(default_response_class=XmlAppResponse)
app.router.route_class = XmlRoute


@app.post("/order", response_model=Order)
def create_order(order: Order = XmlBody()) -> Order:
    return order


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app)

API

Symbol Description
XmlBody() Mark a parameter as coming from XML body
XmlRoute Route class that enables XML handling
XmlAppResponse Response class for application/xml
XmlTextResponse Response class for text/xml
add_openapi_extension Patches OpenAPI schema for XML types

Requirements

  • Python 3.10+
  • FastAPI >= 0.113.0
  • xsdata >= 22.9

License

MIT

Metadata

Release files for fastapi-xml 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-xml 1.1.2
File Size Uploaded
fastapi_xml-1.1.2.tar.gz 13.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-xml 1.1.2
File Interpreter ABI Platform
fastapi_xml-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 28.3 kB

Release files / fastapi_xml-1.1.2.tar.gz

Download URL fastapi_xml-1.1.2.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a54eb2e83795edd981e93c4d10862fce956cfe37527555cd1ca9363608b9a1cb
BLAKE2b-256 checksum
How to use checksums
150564d218f67f9d5f94a4f2b27796ffd5db28d5965aa1d20ffc3360473beaa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / fastapi_xml-1.1.2-py3-none-any.whl

Download URL fastapi_xml-1.1.2-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
322bf5545cccf7973d9f001652b99c05b1e33a1fc03cf05896c65c7fbf989ae0
BLAKE2b-256 checksum
How to use checksums
80398d0be531ed24a59b3ac8eb927021b44171d705f61f5cab6e41f23f7e7898
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page