{"record":{"id":"f25b7ddf4532f854","repo":"pallets/flask","slug":"unable-to-build-urls-outside-an-active-request-wit","errorCode":null,"errorMessage":"Unable to build URLs outside an active request without 'SERVER_NAME' configured. Also configure 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as needed.","messagePattern":"Unable to build URLs outside an active request without 'SERVER_NAME' configured\\. Also configure 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as needed\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/flask/app.py","lineNumber":1188,"sourceCode":"                    endpoint = f\"{blueprint_name}{endpoint}\"\n                else:\n                    endpoint = endpoint[1:]\n\n            # When in a request, generate a URL without scheme and\n            # domain by default, unless a scheme is given.\n            if _external is None:\n                _external = _scheme is not None\n        else:\n            # If called by helpers.url_for, an app context is active,\n            # use its url_adapter. Otherwise, app.url_for was called\n            # directly, build an adapter.\n            if ctx is not None:\n                url_adapter = ctx.url_adapter\n            else:\n                url_adapter = self.create_url_adapter(None)\n\n            if url_adapter is None:\n                raise RuntimeError(\n                    \"Unable to build URLs outside an active request\"\n                    \" without 'SERVER_NAME' configured. Also configure\"\n                    \" 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as\"\n                    \" needed.\"\n                )\n\n            # When outside a request, generate a URL with scheme and\n            # domain by default.\n            if _external is None:\n                _external = True\n\n        # It is an error to set _scheme when _external=False, in order\n        # to avoid accidental insecure URLs.\n        if _scheme is not None and not _external:\n            raise ValueError(\"When specifying '_scheme', '_external' must be True.\")\n\n        self.inject_url_defaults(endpoint, values)\n","sourceCodeStart":1170,"sourceCodeEnd":1206,"githubUrl":"https://github.com/pallets/flask/blob/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/app.py#L1170-L1206","documentation":"Raised by Flask.url_for() (outside a request context) when no URL adapter can be constructed. Outside a request, Flask needs SERVER_NAME (and typically APPLICATION_ROOT/PREFERRED_URL_SCHEME) to build an absolute URL adapter; without them url_adapter is None.","triggerScenarios":"Calling app.url_for('endpoint') or url_for('endpoint', _external=True) in a script, background task, or test without a request context and without SERVER_NAME set in app.config.","commonSituations":"Generating URLs in a CLI command, celery task, or test fixture; deploying behind a proxy without setting SERVER_NAME; calling url_for at import time.","solutions":["Set SERVER_NAME in config: app.config['SERVER_NAME'] = 'example.com:5000'.","Wrap the call in a request/test context: with app.test_request_context(): url_for('endpoint').","Use app.app_context() plus SERVER_NAME when only an app context is available."],"exampleFix":"// before\n# outside any request, no SERVER_NAME\nurl = app.url_for('profile', user_id=5)  # raises\n// after\napp.config['SERVER_NAME'] = 'example.com'\nurl = app.url_for('profile', user_id=5)","handlingStrategy":"validation","validationCode":"if not app.config.get('SERVER_NAME'):\n    raise RuntimeError('SERVER_NAME must be set to build URLs outside a request')\napp.url_for('endpoint')","typeGuard":"def can_build_url_offline(app) -> bool:\n    return bool(app.config.get('SERVER_NAME'))","tryCatchPattern":"try:\n    url = app.url_for('endpoint')\nexcept RuntimeError as e:\n    if 'SERVER_NAME' in str(e):\n        url = build_urlManually(...)\n    raise","preventionTips":["Set SERVER_NAME in config for offline URL generation.","Use test_request_context() in tests and scripts.","Avoid calling url_for at import time."],"tags":["url-building","configuration","context"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}