Skip to content

What is the right way to call an APIRouter endpoint from another in same app #2748

@jintolonappan

Description

@jintolonappan

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Question

I'm working on creating an application by following the tutorial. I currently have a structure similar
to below as mentioned in Bigger Applications - Multiple Files section of the tutorial:

.
├── app
│   ├── __init__.py
│   ├── main.py
│   ├── dependencies.py
│   └── routers
│   │   ├── __init__.py
│   │   ├── items.py
│   │   └── users.py
│   └── internal
│       ├── __init__.py
│       └── admin.py

I have a usecase where I need to hit one of the endpoint exposed by one APIRouter (Say, in this example users from the other APIRouter (items from the example).

This is required due to some of the foreign key relationships and wanted to POST the data to referenced table rather than throwing an exception back to the user.
Using above example, the end consumer may send both user data and items data in a single API call and internally, my app needs to hit both users endpoint and items endpoint.

I already looked at the Requests section and thought there must be a better way rather than figuring out client host and manually coming up with the API endpoint.
I also looked at Callback section.

I'm using SQLAlchemy for database connections and is referenced in each APIRouters as Depends.

Environment

  • OS: macOS Catalina 10.15.7
  • FastAPI Version: 0.63.0
  • Python version: Python 3.8.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions