Skip to main content

VSCode - Expose a local API publicly using port forwarding

I’m currently working on building my own Copilot agent(more about this in another post). As part of the process, I needed to create an API and expose it publicly so it is accessible publicly through a GitHub app. During local development and debugging I don't want to have to publish my API, so let's look at how we can use the VS Code Port Forwarding feature to expose a local API publicly.

Port forwarding

Port forwarding is a networking technique that redirects communication requests from one address and port combination to another. In the context of web development and VS Code, here's what it means:

When you run a web application or API locally, it's typically only accessible from your own machine at addresses like localhost:3000 or 127.0.0.1:8080. Port forwarding creates a tunnel that takes requests coming to a publicly accessible address and forwards them to your local port.

For example, if you have an API running locally on port 3000:

This is useful for:

  • Sharing your in-development work with clients or teammates
  • Testing webhook integrations that need to reach your local environment
  • Debugging mobile apps that need to communicate with your local backend
  • Accessing your local development environment from another device

Using port forwarding in VS Code

I started by opening VS Code and creating a new .NET Core Web API project:

dotnet new  webapi

This creates a new example Web API project using the Minimal API feature exposing a single ‘weatherforecast’ endpoint:

Once the project is created, check the launchsettings.json for the default port used:

Now debug or run your application. Once it is running, go to the Ports tab:

Hit the Forward a Port button and enter the port number you want to forward. The first time you do this; you are requested to sign in on GitHub. Click on Allow and go through the authentication flow.

Now you get a publicly accessible endpoint. 

By default this endpoint is set to private. This means that you need to authenticate when you try to open the URL in your browser:

 

If you don’t want this, you can change the endpoint to Public. Right click on the Endpoint and use the Port Visibility menu item from the context menu:

This removes the requirement to sign-in. Be careful when you do this as everyone is able to access your endpoint now.

 

Remark: A similar feature exists in Visual Studio where it is called Dev Tunnels.

More information

Local Port Forwarding

Visual Studio Dev Tunnels

Popular posts from this blog

.NET 8–Keyed/Named Services

A feature that a lot of IoC container libraries support but that was missing in the default DI container provided by Microsoft is the support for Keyed or Named Services. This feature allows you to register the same type multiple times using different names, allowing you to resolve a specific instance based on the circumstances. Although there is some controversy if supporting this feature is a good idea or not, it certainly can be handy. To support this feature a new interface IKeyedServiceProvider got introduced in .NET 8 providing 2 new methods on our ServiceProvider instance: object? GetKeyedService(Type serviceType, object? serviceKey); object GetRequiredKeyedService(Type serviceType, object? serviceKey); To use it, we need to register our service using one of the new extension methods: Resolving the service can be done either through the FromKeyedServices attribute: or by injecting the IKeyedServiceProvider interface and calling the GetRequiredKeyedServic...

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

Kubernetes–Limit your environmental impact

Reducing the carbon footprint and CO2 emission of our (cloud) workloads, is a responsibility of all of us. If you are running a Kubernetes cluster, have a look at Kube-Green . kube-green is a simple Kubernetes operator that automatically shuts down (some of) your pods when you don't need them. A single pod produces about 11 Kg CO2eq per year( here the calculation). Reason enough to give it a try! Installing kube-green in your cluster The easiest way to install the operator in your cluster is through kubectl. We first need to install a cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.5/cert-manager.yaml Remark: Wait a minute before you continue as it can take some time before the cert-manager is up & running inside your cluster. Now we can install the kube-green operator: kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml Now in the namespace where we want t...