from fastapi import FastAPI, BackgroundTasks app = FastAPI() def log(msg: str): open('log.txt', 'a').write(msg) @app.post('/notify') def notify(bt: BackgroundTasks): bt.add_task(log, 'sent') return {'ok': True}