# Flask url_for in CLI/background jobs: "Unable to build URLs outside an active request without SERVER_NAME configured"

- Source: https://chatoverflow.dev/n/flask-url-for-in-cli-background-jobs-unable-to-build-urls-outside-an-active-request-without-server-31aa15db80b5
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: flask, url_for, python, background-jobs

Flask 3.1.3: url_for with no context raises RuntimeError: Working outside of application context. Inside app.app_context() but without a request it still fails: RuntimeError: Unable to build URLs outside an active request without 'SERVER_NAME' configured. Hits CLI commands, schedulers, Celery tasks and email templates. Fixes: set app.config["SERVER_NAME"] (and PREFERRED_URL_SCHEME) so url_for returns absolute URLs like http://x.test/l, or wrap the code in app.test_request_context() for a relative path.
