{"id":28092389,"url":"https://github.com/priyanshu-panwar/fastapi-utilities","last_synced_at":"2025-05-13T13:10:02.476Z","repository":{"id":198162042,"uuid":"699683731","full_name":"priyanshu-panwar/fastapi-utilities","owner":"priyanshu-panwar","description":"🎨⚡️🔥 Reusable Utilities for FastAPI","archived":false,"fork":false,"pushed_at":"2025-04-24T16:57:37.000Z","size":233,"stargazers_count":50,"open_issues_count":9,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-02T14:10:24.520Z","etag":null,"topics":["cli","fastapi","fastapi-boilerplate","fastapi-cli","fastapi-template","fastapi-utilities","fastapi-utils"],"latest_commit_sha":null,"homepage":"https://youtu.be/ZIggeTU8JhQ?si=SO1B0Is0RdXDkbCa","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/priyanshu-panwar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":"FUNDING.yml","license":"LICENSE","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},"funding":{"github":{"funding":[{"type":"individual","url":"https://github.com/sponsors/priyanshu-panwar"}]}}},"created_at":"2023-10-03T06:07:19.000Z","updated_at":"2025-04-16T18:16:14.000Z","dependencies_parsed_at":"2024-10-27T17:46:20.704Z","dependency_job_id":"137d18c2-cdc1-4f8c-bfc3-bb3d1fa9f33b","html_url":"https://github.com/priyanshu-panwar/fastapi-utilities","commit_stats":null,"previous_names":["priyanshu-panwar/fastapi-utilities"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/priyanshu-panwar%2Ffastapi-utilities","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/priyanshu-panwar%2Ffastapi-utilities/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/priyanshu-panwar%2Ffastapi-utilities/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/priyanshu-panwar%2Ffastapi-utilities/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/priyanshu-panwar","download_url":"https://codeload.github.com/priyanshu-panwar/fastapi-utilities/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253948437,"owners_count":21988957,"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":["cli","fastapi","fastapi-boilerplate","fastapi-cli","fastapi-template","fastapi-utilities","fastapi-utils"],"created_at":"2025-05-13T13:09:05.408Z","updated_at":"2025-05-13T13:10:02.464Z","avatar_url":"https://github.com/priyanshu-panwar.png","language":"Python","funding_links":["https://github.com/sponsors/[\"funding\", [{\"type\" =\u003e \"individual\", \"url\" =\u003e \"https://github.com/sponsors/priyanshu-panwar\"}]]"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003cem\u003e🎨⚡️🔥 Reusable Utilities for FastAPI\u003c/em\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://img.shields.io/github/last-commit/priyanshu-panwar/fastapi-utilities.svg\" /\u003e\n\u003ca href=\"https://github.com/priyanshu-panwar/fastapi-utilities/actions/workflows/build.yaml\" \u003e \n \u003cimg src=\"https://github.com/priyanshu-panwar/fastapi-utilities/actions/workflows/build.yaml/badge.svg\"/\u003e \n \u003c/a\u003e\n\u003ca href=\"https://codecov.io/gh/priyanshu-panwar/fastapi-utilities\" \u003e \n \u003cimg src=\"https://codecov.io/gh/priyanshu-panwar/fastapi-utilities/graph/badge.svg?token=8ACG93WM6I\"/\u003e \n \u003c/a\u003e\n\u003cbr /\u003e\n\u003ca href=\"https://pypi.org/project/fastapi-utilities\" target=\"_blank\"\u003e\n\u003cimg src=\"https://badge.fury.io/py/fastapi-utilities.svg\" alt=\"Package version\"\u003e\n\u003c/a\u003e\n\u003cimg alt=\"PyPI - Python Version\" src=\"https://img.shields.io/pypi/pyversions/fastapi-utilities\"\u003e\n\u003cimg alt=\"PyPI - Python Version\" src=\"https://img.shields.io/github/license/priyanshu-panwar/fastapi-utilities.svg\"\u003e\n\u003cbr /\u003e\n\u003ca href=\"https://pepy.tech/project/fastapi-utilities\" \u003e \n \u003cimg src=\"https://static.pepy.tech/badge/fastapi-utilities\"/\u003e \n \u003c/a\u003e\n\u003ca href=\"https://pepy.tech/project/fastapi-utilities\" \u003e \n \u003cimg src=\"https://static.pepy.tech/badge/fastapi-utilities/month\"/\u003e \n \u003c/a\u003e\n\u003ca href=\"https://pepy.tech/project/fastapi-utilities\" \u003e \n \u003cimg src=\"https://static.pepy.tech/badge/fastapi-utilities/week\"/\u003e \n \u003c/a\u003e\n\u003c/p\u003e\n\n---\n**Features**:\n- Repeat At / Every for scheduling cron jobs\n- TTL LRU Cache\n- Timing Middleware that logs you the time taken by a request\n- Session Middleware\n- CLI tool to generate skeleton\n\n\n---\n\n**Source Code**: \u003ca href=\"https://github.com/priyanshu-panwar/fastapi-utilities\" target=\"_blank\"\u003ehttps://github.com/priyanshu-panwar/fastapi-utilities\u003c/a\u003e  \n**Youtube Link**: [Click Here](https://youtu.be/ZIggeTU8JhQ?si=SO1B0Is0RdXDkbCa)\n\n_Inspired From_: \u003ca href=\"https://github.com/dmontagu/fastapi-utils\" target=\"_blank\"\u003edmontagu/fastapi-utils\u003c/a\u003e\n\n---\n\n## [✨Update✨] How to use with Latest FastAPI version\n\nWith the latest FastAPI version, `on_event` lifespan functions are depreceated. Here is the official [doc](https://fastapi.tiangolo.com/advanced/events/#async-context-manager).\nWe need to make use of `asynccontextmanager` with the latest fastapi.\n\nHere is an example how to use lifespan (Repeated Tasks) functions with latest fastapi:\n\n```\nfrom fastapi import FastAPI\nfrom contextlib import asynccontextmanager\nfrom fastapi_utilities import repeat_every, repeat_at\n\n@asynccontextmanager\nasync def lifespan(app: FastAPI):\n    # --- startup ---\n    await test()\n    test2()\n    yield\n    # --- shutdown ---\n\napp = FastAPI(lifespan=lifespan)\n\n# Repeat Every Example\n@repeat_every(seconds=2)\nasync def test():\n    print(\"test\")\n\n# Repeat At Example\n@repeat_at(cron=\"* * * * *\")\ndef test2():\n    print(\"test2\")\n```\n\nOnly difference is to call our tasks from lifespan function instead of using `on_event` function.\n\n---\n\n## [🔥New🔥] TTL LRU CACHE\n\nWe have introduced `ttl_lru_cache` now in our library.\n\n### How to use\n\n```\nfrom fastapi_utilities import ttl_lru_cache\n\n@ttl_lru_cache(ttl=2, max_size=128)\ndef sum(a: int, b: int) -\u003e int:\n    return a + b\n\nsum(1, 3)\nsum(1, 3)\n```\n\n---\n\n## [🔥New🔥] FastAPI CLI Tool\n\nWith our CLI Tool you can get a skeleton project built to get you started with the code.\n\n### How to use\n\n- Using `poetry`: `poetry run cli init`\n- Using `pip`: `python3 -m cli init`\n\n---\n\n## Features\n\nThis package includes a number of utilities to help reduce boilerplate and reuse common functionality across projects:\n\n- **🕒Repeated Tasks**: Easily trigger periodic tasks on server startup using **repeat_every**.\n\n```\n\nfrom fastapi_utilities import repeat_every\n\n@router.on_event('startup')\n@repeat_every(seconds=3)\nasync def print_hello():\n\n    print(\"hello\")\n```\n\n- **👷Cron Jobs**: Easily trigger cron jobs on server startup using **repeat_at** by providing a cron expression.\n\n```\n\nfrom fastapi_utilities import repeat_at\n\n@router.on_event(\"startup\")\n@repeat_at(cron=\"*/2 * * * *\") #every 2nd minute\nasync def hey():\n    print(\"hey\")\n\n```\n\n- **🕒Timer Middleware**: Add a middleware to the FastAPI app that logs the time taken to process a request. Optionally, also logs the average response time.The average response time is reset after every (reset_after)100,000 requests.\n\n```\n\nimport asyncio\nfrom fastapi import FastAPI, Request\nfrom fastapi_utilities import add_timer_middleware\n\napp = FastAPI()\nadd_timer_middleware(app, show_avg=True)\n\n\n@app.get(\"/\")\ndef read_root():\n    return {\"message\": \"Hello, World!\"}\n\n```\n\nResponse Logs:\n\n```\nINFO:     (fastapi-utilities) \"GET - /\" :: Time Taken :: 0.97 ms\nINFO:     :: Average Response Time :: 0.97 ms\n```\n\n- **Cached Sessions**: Now use cached sessions along with context manager instead of `get_db`.\n\n```\nfrom fastapi import FastAPI\nfrom .db import Base, engine\nfrom fastapi_utilities import FastAPISessionMaker, repeat_every\nfrom .models import User\nimport random\n\napp = FastAPI()\nBase.metadata.create_all(bind=engine)\n\nsession_maker = FastAPISessionMaker(\"sqlite:///db.sqlite3\")\n\n\n@app.on_event(\"startup\")\n@repeat_every(seconds=5, raise_exceptions=True)\nasync def startup():\n    print(\"Starting up...\")\n    with session_maker.context_session() as session:\n        x = User(id=random.randint(0, 10000))\n        session.add(x)\n    print(\"Startup complete!\")\n\n```\n\n---\n\n## Requirements\n\nThis package is intended for use with any recent version of FastAPI and Python 3.7+.\n\n## Installation\n\n```bash\npip install fastapi-utilities\n```\n\n## License\n\nThis project is licensed under the terms of the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpriyanshu-panwar%2Ffastapi-utilities","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpriyanshu-panwar%2Ffastapi-utilities","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpriyanshu-panwar%2Ffastapi-utilities/lists"}