{"id":16356571,"url":"https://github.com/paul121/fastapi-zeit-now","last_synced_at":"2025-03-21T00:31:32.743Z","repository":{"id":49713894,"uuid":"245077206","full_name":"paul121/fastapi-zeit-now","owner":"paul121","description":"A simple example of deploying FastAPI as a Zeit Serverless Function","archived":false,"fork":false,"pushed_at":"2024-02-05T17:54:45.000Z","size":12,"stargazers_count":28,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-15T00:14:09.544Z","etag":null,"topics":["asgi","fastapi","serverless","starlette","zeit"],"latest_commit_sha":null,"homepage":"https://fastapi-zeit-now.now.sh","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paul121.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-05T05:29:29.000Z","updated_at":"2024-08-12T07:58:24.000Z","dependencies_parsed_at":"2024-10-28T09:11:37.607Z","dependency_job_id":"a186c666-e2b5-4103-bc41-7ec146ba2500","html_url":"https://github.com/paul121/fastapi-zeit-now","commit_stats":{"total_commits":9,"total_committers":2,"mean_commits":4.5,"dds":"0.11111111111111116","last_synced_commit":"7a0b3de45f908c2cb7a71d9d13d0b4257d09d31b"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paul121%2Ffastapi-zeit-now","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paul121%2Ffastapi-zeit-now/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paul121%2Ffastapi-zeit-now/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paul121%2Ffastapi-zeit-now/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paul121","download_url":"https://codeload.github.com/paul121/fastapi-zeit-now/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244717377,"owners_count":20498283,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asgi","fastapi","serverless","starlette","zeit"],"created_at":"2024-10-11T01:43:52.840Z","updated_at":"2025-03-21T00:31:32.433Z","avatar_url":"https://github.com/paul121.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FastAPI Zeit Now\n\nDeploy a [FastAPI] app as a [Zeit] Serverless Function.\n\nThis repo deploys the [FastAPI SQL Databases Tutorial] to demonstrate how a FastAPI app can be deployed using the Zeit \n[Asynchronous Server Gateway Interface (ASGI)].\n\n**_View the live demo at: [https://fastapi-zeit-now.paul121.now.sh/?name=GithubUser](https://fastapi-zeit-now.paul121.now.sh/?name=GithubUser)_**\n- OpenAPI Docs: [https://fastapi-zeit-now.paul121.now.sh/docs](https://fastapi-zeit-now.paul121.now.sh/docs)\n- Try the API endpoints [/?name=Name](https://fastapi-zeit-now.paul121.now.sh/?name=Name), [/users](https://fastapi-zeit-now.paul121.now.sh/users), [/items](https://fastapi-zeit-now.paul121.now.sh/items)\n\nNotes about this deployment:\n- FastAPI is configured to return a `Cache-Control` header set to `no-cache` for all responses. Because\nstatic caching is automatic with Zeit, this ensures the Zeit CDN doesn't cache anything for the purposes of this \nexample. More on caching [here](https://zeit.co/docs/v2/network/caching).\n- This repo contains a sample sqlite database that has a few predefined `users` and `items` to demonstrate returning data\nfrom a database.\n  - **Note** Due to the nature of a serverless deploy, the sqlite file cannot be written to so any \n*POST requests attempting to modify the DB will fail.*\n  - In a production deploy, the FastAPI app would connect to a \ndatabase hosted elsewhere.\n\nThis is merely an example of integration with Zeit. I'm not currently deploying any FastAPI apps in this way, but would\nlike to consider it a possibility. **_Any thoughts, concerns or ideas for benchmarking are welcome!!_**\n\n## Background\n\nAfter learning about [Zappa] I was inspired to learn more about hosting FastAPI as a server-less function:\n\u003e Zappa makes it super easy to build and deploy server-less, event-driven Python applications (including, but not limited to, WSGI web apps) on AWS Lambda + API Gateway. Think of it as \"serverless\" web hosting for your Python apps. That means infinite scaling, zero downtime, zero maintenance - and at a fraction of the cost of your current deployments!\n\nThe problem is that Zappa only works with WSGI Python apps such as Flask and Django, not ASGI.\n\nGoogle Cloud Run and AWS Elastic Beanstalk are other alternatives, but don't support ASGI either.\n\nI came across [Mangum] which is similar to Zappa, except it supports ASGI apps. While this would likely work with \nFastAPI (or most any ASGI Python app) it also seems to make some decisions about how you structure your app. And it \nstill requires quite a bit of configuration with AWS to get everything working. (more in this [issue](https://github.com/tiangolo/fastapi/issues/812))\n\n[Zeit Now] makes this all a bit easier. Develop locally with `now dev` and deploy with `now --prod`.\n\n## Configuration\n\nWith [Zeit Now], we just need to configure a few things in `now.json`, run `now --prod` and FastAPI is deployed.\n\n*See Zeit Docs on [Configuration](https://zeit.co/docs/configuration)*\n\n### Requirements\n\nDefine `pip install` requirements in a `requirements.txt` file.\n\n### Routing\n\nLooking into this, the hardest thing to configure was [Zeit Routes]: \n\u003e By default, routing is defined by the filesystem of your deployment. For example, if a user makes a request to /123.png, and your now.json file does not contain any routes with a valid src matching that path, it will fallback to the filesystem and serve /123.png if it exists.\n\nThis makes great sense for most serverless apps, but doesn't work so well with FastAPI when one function needs to \nrespond to multiple routes (figured out in this [issue](https://github.com/zeit/now/issues/3729#issuecomment-582114686)).\n\nI couldn't get `rewrite` to work, but did have success routing all requests to one FastAPI function:\n```json\n  \"routes\": [\n    { \"src\": \"/(.*)\", \"dest\": \"app/main.py\" }\n  ]\n```\n\n### Defining Functions\n\nBy default Zeit also looks for Python apps in an `index.py` file at the root or in the `/api` directory. This is can be\nconfigured by adding a `build` configuration to `now.json`:\n```json\n  \"builds\": [\n    { \"src\": \"/app/main.py\", \"use\": \"@now/python\" }\n  ]\n```\n\n\n[FastAPI]: https://fastapi.tiangolo.com\n[FastAPI SQL Databases Tutorial]:https://fastapi.tiangolo.com/tutorial/sql-databases/#review-all-the-files\n[Zeit]: https://zeit.co\n[Zeit Now]: https://github.com/zeit/now/tree/master/packages/now-cli\n[Asynchronous Server Gateway Interface (ASGI)]: https://zeit.co/docs/runtimes#advanced-usage/advanced-python-usage/asynchronous-server-gateway-interface\n[Zeit Routes]: https://zeit.co/docs/configuration#project/routes\n[Zappa]: https://github.com/Miserlou/Zappa\n[Mangum]: https://github.com/erm/mangum","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaul121%2Ffastapi-zeit-now","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaul121%2Ffastapi-zeit-now","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaul121%2Ffastapi-zeit-now/lists"}