Fastapi: Using FastAPI for coverage existing API

Created on 12 Aug 2020  路  4Comments  路  Source: tiangolo/fastapi

How I can covered existing API?
For example if i use:

import uvicorn
from typing import Optional

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}

if __name__ == '__main__':
    uvicorn.run("api_fastapi_small:app", port=8080, reload=True)

this return localhost calls:
Curl
curl -X GET "http://127.0.0.1:8080/" -H "accept: application/json"
Request URL
http://127.0.0.1:8080/

But if I need send and create documentation for https://somesite.com/api/
How i can change this?
I just need to describe the existing API that can be implemented, for example on PHP and runs on another server.

I also find similar question, but the documents referred to are missing (404) https://github.com/tiangolo/fastapi/issues/1108

answered question

All 4 comments

Check this, I think thats help you
https://fastapi.tiangolo.com/advanced/sub-applications/

It's more about how to use two points for documentation.
But I just need to describe the existing API that can be implemented, for example on PHP and on another server.

Okay, got it.
If you need FastAPI only for documentation I'd consider on something different specific for yout server.
If it's not an option you may use openAPI schema json generated by fastapi on another server (if you able to use swagger). At last, you can make redirect for FastAPI documentation route.
But last two points is overkill imo. That's not why you use fastapi :)

Thanks for the help here @SirTelemak ! :clap: :bow:

If that solves the original problem, then you can close this issue :heavy_check_mark: Otherwise, you can use the ideas here: https://fastapi.tiangolo.com/advanced/extending-openapi/ to extract the autogenerated OpenAPI schema from FastAPI and save it in a JSON file or however you want.

Was this page helpful?
0 / 5 - 0 ratings