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.3

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.3
File Size Uploaded Provenance Actions
fastapi_xml-1.1.3.tar.gz 13.4 kB Without provenance Details for fastapi_xml-1.1.3.tar.gz

Built distribution (wheel)

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

Total release size: 28.8 kB

Release files / fastapi_xml-1.1.3.tar.gz

Download URL fastapi_xml-1.1.3.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e26bc61ea7ea8789455d479e78cc86a2d3f2a2c50962e68b501ef3d663f5dec2
BLAKE2b-256 checksum
How to use checksums
b9159d63728d4bd0cc8f4e93261b48f7355b064c9495b8d2b6b30082cd518947
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.14.4 Linux/7.0.0-38-generic

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

Download URL fastapi_xml-1.1.3-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0081f65a54f4eeb7b4806dd01ea8796654239babb335d05f0f8ab7abd5303194
BLAKE2b-256 checksum
How to use checksums
7446a3498bd1e16034b2cc054afb837fb60d37c7978cd4a73bceb0555893602d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.14.4 Linux/7.0.0-38-generic

Security for fastapi-xml 1.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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