How to remove stage from URLs for AWS Lambda functions Serverless framework?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
With API Gateway and Serverless Framework, the default endpoint usually includes the stage name in the path, such as /prod/users. In practice, you do not remove stages from AWS itself. You keep the stage for deployment purposes and hide it behind a custom domain and base-path mapping so the public URL becomes cleaner.
Why the Stage Appears in the First Place
A default API Gateway URL often looks like this:
The prod segment is the API Gateway stage. Serverless Framework creates and deploys that stage; it does not abolish the underlying AWS routing model. So there are really two different goals:
- keep using stages for deployment separation
- stop exposing the stage name in the public URL
The normal production answer is yes to the first goal and yes to the second.
Use a Custom Domain for the Public URL
The usual fix is to map a custom domain such as api.example.com to the stage. Then callers see a clean URL:
The stage still exists behind the scenes, but clients no longer have to include it in the path.
A common serverless.yml setup looks like this:
The key setting is basePath: '', which maps the stage to the root of the custom domain.
The Lambda Code Does Not Need to Change
Removing the stage from the public URL is a routing concern, not a Lambda-handler concern. A function can stay exactly the same.
The only thing that changes is how API Gateway routes requests to that function.
Separate Environments with Separate Domains
If you have several environments, avoid trying to make one domain ambiguously represent several stages. A cleaner pattern is:
- '
api.example.comfor production' - '
api-staging.example.comfor staging' - '
api-dev.example.comfor development'
This keeps environment boundaries obvious and avoids accidental production traffic hitting the wrong stage.
What You Usually Cannot Change
The raw execute-api.amazonaws.com hostname generally follows API Gateway’s own stage-aware structure. If you want a stage-free public URL, use a custom domain. Expecting the default AWS hostname to behave like a polished production URL is usually the wrong mental model.
It is also worth checking whether your service uses REST API or HTTP API configuration, because the exact Serverless Framework settings can differ slightly between them even though the public-domain idea stays the same.
Common Pitfalls
- Expecting the default
execute-apihostname to hide the stage without any custom-domain setup. - Forgetting
basePath: ''and leaving an extra public path segment in place. - Mapping the wrong stage to the production domain.
- Overcomplicating the Lambda function even though the problem is only in API routing.
- Mixing REST API and HTTP API examples without checking which one your service actually uses.
Summary
- The stage name is part of API Gateway’s default deployment URL structure.
- In Serverless Framework, the practical way to hide it is a custom domain with base-path mapping.
- '
basePath: ''maps the stage to the root of the public domain.' - The Lambda handler code does not need to change for this URL cleanup.
- Separate domains per environment are usually clearer than hiding multiple stages behind one domain.

