How do I get my API Gateway URL?
Tags: apigateway, aws, lambda, serverless
Published: 2020-09-02 21:58:44

---
title: How do I get my API Gateway URL?
published: true
date: 2017-04-07 00:00:00 UTC
tags: apigateway, aws, lambda, #serverless
canonical_url: https://www.goingserverless.com/blog/how-do-i-get-my-api-gateway-url
---

Occasionally you need to know the API Gateway URL for your services inside your Lambda. This happened to me recently when one of my Lambda's needed to provide a callback URL to a third party service that it was using.

It seems that a lot of people are solving this problem by deploying their API's using [Serverless](https://serverless.com/), then copying the URL and redeploying again with that URL hard coded into their `serverless.yml` as an environment variable.

If you're one of those people then **STOP**. This is both the hard way and it has two very negative side effects.

<!-- more -->
1. It's difficult to deploy your application to other stages, and
2. It introduces a risk that you'll break something by depolying to one stage with the API Gateway URL for the different stage.

So how do I get around this?

Instead of hard coding the API GW URL you can reconstruct it using a combination of Serverless variables and CloudFormation. Here's how I set the API gateway URL so it's passed to my Lambda as the GW\_URL environment variable.

```
provider:
  environment:
    GW_URL:
      Fn::Join:
        - ""
        - - "https://"
          - Ref: "ApiGatewayRestApi"
          - ".execute-api.${self:custom.region}.amazonaws.com/${self:custom.stage}"
```

Inside my code I can now reference the current API GW URL using.

```
process.env.GW_URL;
```

If you're using the [serverless-offline plugin](https://github.com/dherault/serverless-offline) (or similar) then you will probably want to combine this with [per stage environment variables](%7B%7B%20site.baseurl%20%7D%7D/blog/using-environment-variables-with-the-serverless-framework). You'll need to use per stage environment variables because this approach assumes you're deploying with CloudFormation but running it locally means no CloudFormation to resolve the URL. Instead you'll need to hard code a URL for local development.

The "cheat sheet" version is below but I strongly recommend you read [the post](%7B%7B%20site.baseurl%20%7D%7D/blog/using-environment-variables-with-the-serverless-framework) as the full version explains it in more depth and covers topics like having separate files for each environment.

```
provider:
  environment:
    GW_URL: ${self:provider.${self:custom.stage}.GW_URL}

custom:
  stage: "${opt:stage, self:provider.stage}"
  prod:
    GW_URL:
      {
        "Fn::Join":
          [
            "",
            [
              "https://",
              { "Ref": "ApiGatewayRestApi" },
              ".execute-api.${self:custom.region}.amazonaws.com/${self:custom.stage}",
            ],
          ],
      }
  dev:
    GW_URL: "http://localhost:3000/"
```

Using this technique you can also export your API Gateway URL from your CloudFormation stack so that it can be imported into other stacks. This is one way to solve the service discovery problem.

```
resources:
  Outputs:
    ApiUrl:
      Description: "The API Gateway URL"
      Value:
        Fn::Join:
          - ""
          - - "https://"
            - Ref: ApiGatewayRestApi
            - ".execute-api.${self:custom.region}.amazonaws.com/${self:custom.stage}"
```