---
"title": "Mastering API Gateway Integration: Salesforce, Heroku, and MuleSoft Anypoint Flex Gateway"
"description": "Securely expose and manage your APIs. Deploy the MuleSoft Flex API Gateway on Heroku to connect and protect private services."
"date": "2024-07-29"
"authors":
  -
    "name": "Julián Duque"
"url": "https://wp-www-staging.heroku.com/blog/mastering-api-integration-salesforce-heroku-mulesoft-anypoint-flex-gateway/"
"categories":
  - "Engineering"
"tags":
  - "APIs"
  - "App Architecture"
  - "Developer Tools"
  - "Integrations"
  - "Salesforce"
---

# Mastering API Gateway Integration: Salesforce, Heroku, and MuleSoft Anypoint Flex Gateway

In today’s fast-paced digital world, companies are looking for ways to expose their APIs and microservices to the internet while enhancing their overall API security. MuleSoft Anypoint Flex Gateway is a powerful solution that solves this problem. Let’s walk through deploying the Anypoint Flex Gateway on Heroku in a few straightforward steps. You’ll learn how to connect your private APIs and microservices on the Heroku platform through the [Anypoint Flex Gateway](https://www.mulesoft.com/platform/api/flex-api-gateway) which provide comprehensive API management capabilities without the hassle of managing infrastructure. Get ready to unlock the potential of this potent pairing and, in the future, integrate it with Salesforce.

## API Gateway deployment with Heroku and Salesforce

Salesforce’s ecosystem provides a seamless, integrated platform for our customers. The most recent MuleSoft Anypoint Flex Gateway release is now compatible with Heroku, offering a powerful cloud API gateway with an improved security profile and reduced latency for APIs hosted on Heroku. By deploying the Anypoint Flex Gateway inside the same [Private Space](https://wp-www-staging.heroku.com/private-spaces/) as your Heroku apps, you create an environment where your Heroku apps with [internal routing](https://wp-www-staging.heroku.com/blog/private-spaces-internal-routing/) can be exposed to the public through the Flex Gateway. This straightforward API gateway deployment adds an extra layer of control. It simplifies compliance with API security best practices by only allowing traffic to flow through the Flex Gateway, which can be configured easily from the MuleSoft control plane and scaled with the simplicity of Heroku. The joint integration simplifies operations and scalability and accelerates your time to value for your Salesforce solutions. ![The MuleSoft Anypoint Flex Gateway connects Salesforce and users to three secured APIs within a Heroku Private Space.](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721934074-heroku-flex-gateway.png "Architecture exposing multiple private APIs on Heroku through Flex Gateway")

## What is Anypoint Flex Gateway?

MuleSoft Anypoint Flex Gateway is a lightweight, ultrafast API Gateway that simplifies the process of building, securing, and managing APIs in the cloud. It removes the burden of API protection, enabling organizations to focus on delivering exceptional digital experiences. Built on the [Anypoint Platform](https://www.mulesoft.com/platform/enterprise-integration), Flex Gateway provides comprehensive API management and governance capabilities for APIs exposed in the cloud. Anypoint Flex Gateway offers robust API security features, including authentication, authorization, and encryption, to safeguard sensitive data. This secure gateway solution empowers you with granular traffic management, enabling control over API traffic flow and the enforcement of rate-limiting policies to maintain service availability. Moreover, Flex Gateway works with API Manager, MuleSoft’s centralized cloud-based API control plane, to deliver valuable analytics and insights into API usage, facilitating data-driven decisions and the optimization of API strategies. Flex Gateway and API Manager are key parts of MuleSoft’s [universal API Management](https://www.mulesoft.com/platform/api-management) capabilities to discover, build, govern, protect, manage, and engage with any API. In conclusion, MuleSoft Anypoint Flex Gateway is an essential API gateway resource for organizations seeking to seamlessly integrate and secure their APIs with Heroku and manage them effectively in a Heroku Private Space. Heroku’s fully managed service, combined with robust security, traffic management, and analytics capabilities, empowers businesses to confidently embrace the cloud and deliver exceptional API experiences to their users.

## Setting up Flex Gateway on Heroku

To get started with MuleSoft Anypoint Flex Gateway on Heroku, you will need to:

1. Create a [Heroku account](https://signup.heroku.com/)
2. Create an [Anypoint Platform account](https://anypoint.mulesoft.com/login/signup)
3. Install the [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli)
4. Install [Docker](https://docs.docker.com/get-docker/) to register the Flex Gateway

Upon completing these steps, you are now ready to begin the setup process. The process is described as follows:

1. Deploy an API in a Heroku [Private Space](https://wp-www-staging.heroku.com/private-spaces/)
2. Create an API specification in Anypoint Design Center
3. Register the Flex Gateway in Runtime Manager
4. Deploy the Flex Gateway to Heroku
5. Connect the Private API to the Flex Gateway

Now let’s detail each step so you can learn how to implement this pattern for your enterprise applications.

## Deploy an API in a Heroku Private Space

*Note: To learn how to create a Heroku Private Space, please refer to the [documentation](https://devcenter.heroku.com/articles/private-spaces). For our example, we already have a private space called `flex-gateway-west`.* Let’s take one of our [reference applications](https://github.com/heroku-reference-apps/openapi-fastify-jwt) as our example, which exposes a REST API with OpenAPI support. Before we deploy the app, we must ensure that it is created as an internal application within the private space. You can deploy this internal application using the **Deploy to Heroku** button or the Heroku CLI. ![Deploying an internal application to a Private Space using the UI](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721934591-Cursor_and_Create_New_App___Heroku.png "Deploying an internal application to a Private Space using the UI") When using the Heroku CLI, make sure you set the `--internal-routing` flag:

```bash
heroku create employee-directory-api --space flex-gateway-west --internal-routing
```

Next, you will proceed to configure the application and any add-ons required. In our example, we need to provision a private database (`heroku-postgresql:private-0`) and set up an RSA public key for JWT authentication support, but these steps might differ for your application. Consult the reference application’s [README](https://github.com/heroku-reference-apps/openapi-fastify-jwt/blob/main/README.md) for a more detailed guide. Once you’ve deployed the app, grab the application URL from the settings page in your Heroku Dashboard. You’ll need this for a later step. ![Heroku dashboard showing the internal app URL for a secure API with custom domain setup instructions.](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721934690-employee-directory-api_·_Settings___Heroku.png "Internal application URL from Heroku Dashboard")

## Create an API specification in Anypoint Design Center

To link the API with the Flex Gateway, you’ll need to create an API specification in Anypoint Platform using the Design Center and then publish it to Anypoint Exchange. If your API running in Heroku Private Space has an API specification that uses the [OpenAPI 3.0 standard](https://spec.openapis.org/oas/v3.0.0.html), which is supported by Anypoint Platform, you can use it here. If you don’t, you can use [Design Center](https://docs.mulesoft.com/design-center/) to create one from scratch. To learn more, see the [API Designer documentation](https://docs.mulesoft.com/design-center/design-create-publish-api-specs). The User Directory reference application offers both JSON and YAML API specifications for your convenience. Access them in the [openapi](https://github.com/heroku-reference-apps/openapi-fastify-jwt/tree/main/openapi) folder on GitHub. In Design Center, let’s click on Create \> Import from file, select either the YAML or JSON file, and then click on **Import** ![Importing an app.yaml file containing an API specification with the import dialog in Anypoint Design Center.](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721934816-image-1.png) Once you’ve imported your file, check Design Center to see that your spec file is error-free. You can even use the mocking service to test the API and make sure everything looks good. If there are no problems and it’s the right file, go ahead and click on **Publish**. ![Design Center interface displaying an OpenAPI YAML API specification, detailing authentication and endpoints.](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721934866-image-2.png) Add the finishing touches to your metadata, like API version and LifeCycle State, then click on **Publish to Exchange**. ![Publishing to Exchange with fields for Asset version, API version), LifeCycle State options, and related semantic versioning help links.](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721934908-image-3.png) Now, with your API specification in hand, let’s move on to registering and deploying the Anypoint Flex Gateway to Heroku. ![API Published to Exchange](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721934939-image-4.png)

## Register the MuleSoft Flex Gateway in Runtime Manager

Before you deploy to Heroku, you need to get the `registration.yaml` configuration file. To do that, go to the Runtime Manager \> Flex Gateways and click **Add Gateway**. Then select Container \> Docker and follow the instructions to set up your gateway locally using Docker. Just follow steps 1 and 2, and that will create the `registration.yaml` file you need. ![Flex Gateway registration instructions](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935099-Anypoint_Management_Center.png) Once the command has been executed, you’ll see the `registration.yaml` file. This file is needed on the next step, along with the confirmation of the gateway listed in your Runtime Manager. ![Flex Gateway registered in Runtime Manager](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935133-image-5.png)

## API Gateway Deployment to Heroku

Now, let’s get the Flex Gateway deployed to Heroku. You can find a reference application for the [Heroku Docker Flex Gateway](https://github.com/heroku-reference-apps/heroku-docker-flex-gateway) on GitHub. There, you have two options: use the **Deploy to Heroku** button for a quick and easy deployment, or follow the detailed **Manual Deployment** instructions in the [README](https://github.com/heroku-reference-apps/heroku-docker-flex-gateway/blob/main/README.md) using the Heroku CLI. Just ensure you’re setting up the Flex Gateway in the same Private Space as the internal API you deployed in earlier steps. For our example, we will use the Heroku CLI, naming our Flex Gateway `api-ingress-west` and deploying to the `flex-gateway-west` private space.

```bash
git clone https://github.com/heroku-reference-apps/heroku-docker-flex-gateway/
cd heroku-docker-flex-gateway
heroku create api-ingress-west --space flex-gateway-west
heroku config:set FLEX_CONFIG="$(cat registration.yaml)" -a api-ingress-west
heroku config:set FLEX_DYNAMIC_PORT_ENABLE=true -a api-ingress-west
heroku config:set FLEX_DYNAMIC_PORT_ENVAR=PORT -a api-ingress-west
heroku config:set FLEX_DYNAMIC_PORT_VALUE=8081 -a api-ingress-west
heroku config:set FLEX_CONNECTION_IDLE_TIMEOUT_SECONDS=60 -a api-ingress-west 
heroku config:set FLEX_STREAM_IDLE_TIMEOUT_SECONDS=300 -a api-ingress-west
heroku config:set FLEX_METRIC_ADDR=tcp://127.0.0.1:2000 -a api-ingress-west
heroku config:set FLEX_SERVICE_ENVOY_DRAIN_TIME=30 -a api-ingress-west
heroku config:set FLEX_SERVICE_ENVOY_CONCURRENCY=1 -a api-ingress-west
heroku stack:set container
git push heroku main
```

You’ll see your Heroku apps deployed to the Private Space. After a minute or so, you should also see the Flex Gateway as connected in Runtime Manager. ![Flex Gateway and API deployed on Heroku Private Spaces](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935247-image-6.png) Make sure to grab the `api-ingress-west` URL under settings like we did with the API. We will need this URL to test things out. ![Flex Gateway registered on Runtime Manager](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935287-image-7.png) And that’s how you deploy the Flex Gateway to Heroku. Now let’s connect our internal API and test it.

## Connect the Private API to the Flex Gateway

Now, the final step is connecting the Private API with Flex Gateway. For this, you will go to Anypoint API Manager and click on **Add API**. ![Add an API to the Flex Gateway](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935343-image-8.png) Then, select the API from Exchange and click on **Next**. ![Select API from Exchange](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935374-image-9.png) Let’s leave the API Downstream default options as they are and move on to setting up the Upstream. Remember the application URL from our initial step? That URL will serve as our **Upstream URL** (using http and no trailing `/`). ![API Upstream configuration](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935414-image-10.png) If everything looks good, go ahead and click on **Save & Deploy**. As the API is not directly accessible due to internal routing, calling it directly will result in a timeout. However, by calling it through the Flex Gateway, you should be able to retrieve the expected response. Let’s proceed with a GET request to `/directory` through the Flex Gateway URL. ![HTTP GET request to the user directory API route using curl](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935467-zsh.png) Or you can view the User Directory OpenAPI documentation from our reference app directly on a web browser by using the same URL. ![User Directory OpenAPI documentation from a web browser](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935494-Swagger_UI.png) Congratulations, you’ve successfully exposed an internal API deployed in Heroku Private Spaces to the outside world through the Anypoint Flex Gateway running on Heroku. Now you can take full advantage of Anypoint’s API management capabilities, including API-level policies for enhanced API security.

## Securing your API with Anypoint Flex Gateway

A common pattern for API authentication is using Client ID Enforcement. You can avoid coding your own solution by utilizing the API Manager to apply policies to your API. In this example, we’ll implement Client ID enforcement to secure the API. To begin, let’s establish an application within Anypoint Platform that will enable us to access the API. Navigate to Exchange, select your API, and in the top right corner, click on **Request access**. Then, pick the API instance where your API is deployed, and select an application to grant access to. If you don’t have one, you can create a new application here and click on **Request access** to obtain the Client and Client Secret credentials. ![Request access form](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935573-image-11.png) Upon your application’s approval, you’ll receive the Client ID and Client Secret. These credentials will be needed for accessing our newly secured API, so be sure to keep them at hand. ![Request access approval window](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935601-User_Directory_API.png) Next, navigate to API Manager, choose the API, and click on **Policies** in the left menu. Click on **Add policy**, then select **Client ID Enforcement** and proceed to **Next**. ![Salesforce API Manager ](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935641-image-12.png) Leave the default configuration for the Client ID Enforcement policy and then click on **Apply**. ![Client ID Enforcement policy configuration](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935672-image-13.png) Now that the policy is active, let’s try a new GET request to the `/directory API` through the Flex Gateway URL. ![HTTP GET request failing with no client\_id](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935703-Cursor_and_DRAFT__Heroku___MuleSoft_Flex_Gateway_Blog_-_Quip.png) Because we’re enforcing the Client ID, we must include it in the request. Let’s purposely use an incorrect one to witness the authentication attempt failure. ![HTTP GET request authentication failing with incorrect client\_id and secret](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935768-zsh-1.png) And finally, let’s get the right Client ID and Client Secret in place to test the authentication. ![HTTP GET request returning the expected values](https://wp-www-staging.heroku.com/wp-content/uploads/2025/03/1721935792-zsh-2.png) This is just one simple but powerful example of one of many policies that you can apply with the API Manager.

## Strategic collaboration for our customers

The Heroku Customer Solutions Architecture (CSA) team, in collaboration with MuleSoft Engineers, played a pivotal role in this Salesforce multi-cloud integration scenario. They listened to customers and got involved in understanding requirements and technical constraints to propose a preliminary proof-of-concept and a series of incremental changes to achieve a perfect match between Heroku and MuleSoft Flex Gateway. [Heroku Enterprise](https://wp-www-staging.heroku.com/enterprise/) customers with Premier or Signature Success Plans can [request in-depth guidance](https://help.heroku.com/enterprise) on this topic from the CSA team. Learn more about [Expert Coaching Sessions here](https://wp-www-staging.heroku.com/support/) or contact your Salesforce account executive.

---

### Learning Resources

- [Anypoint Flex Gateway Overview](https://docs.mulesoft.com/gateway/latest/)
- [Anypoint API Manager documentation](https://docs.mulesoft.com/api-manager/latest/latest-overview-concept)
- [API Management with MuleSoft Demo series](https://www.mulesoft.com/lp/demo/api/api-management-series-mulesoft)
- [Heroku Private Spaces documentation](https://devcenter.heroku.com/articles/private-spaces)

### Authors

#### Julián Duque

Julián is a Principal Developer Advocate at Heroku, with a strong focus on community, education, Node.js, and JavaScript. He loves sharing knowledge and empowering others to become better developers.

#### Parvez Mohamed

Parvez Syed Mohamed is a seasoned product management leader with over 15 years of experience in Cloud Technologies. Currently, as Director of Product Management at MuleSoft/Salesforce, he drives innovation and growth in API protection.

#### Andrea Bernicchia

Andrea Bernicchia is a Senior Customer Solutions Architect at Heroku. He enjoys engaging with Heroku customers to provide solutions for software integrations, architecture patterns, best practices and performance tuning to optimize applications running on Heroku.