Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: Forwarding ports in your codespace
shortTitle: Forward ports
intro: '{% data reusables.codespaces.about-port-forwarding %}'
miniTocMaxHeadingLevel: 3
versions:
fpt: '*'
ghec: '*'
Expand All @@ -11,7 +13,6 @@ topics:
- Codespaces
- Fundamentals
- Developer
shortTitle: Forward ports
---

{% jetbrains %}
Expand All @@ -30,30 +31,39 @@ Port forwarding gives you access to TCP ports running within your codespace. For
{% data reusables.codespaces.navigate-to-ports-tab %}
1. Under the list of ports, click **Add port**.

![Add port button](/assets/images/help/codespaces/add-port-button.png)
![Screenshot of the 'Add port' button](/assets/images/help/codespaces/add-port-button.png)

1. Type the port number or address, then press enter.

![Text box to type port button](/assets/images/help/codespaces/port-number-text-box.png)
![Screenshot of entering a port number](/assets/images/help/codespaces/port-number-text-box.png)

## Using HTTPS forwarding

By default, {% data variables.product.prodname_github_codespaces %} forwards ports using HTTP but you can update any port to use HTTPS, as needed. If you update a port with public visibility to use HTTPS, the port's visibility will automatically change to private.

{% data reusables.codespaces.navigate-to-ports-tab %}
1. Right click the port you want to update, then hover over **Change Port Protocol**.
![Option to change port protocol](/assets/images/help/codespaces/update-port-protocol.png)
1. Right-click the port you want to update, then hover over **Change Port Protocol**.

![Screenshot of the option to change port protocol](/assets/images/help/codespaces/update-port-protocol.png)

1. Select the protocol needed for this port. The protocol that you select will be remembered for this port for the lifetime of the codespace.

{% data reusables.codespaces.port-forwarding-sharing-non-jetbrains %}

{% data reusables.codespaces.navigate-to-ports-tab %}
1. Right click the port that you want to share, select the "Port Visibility" menu, then click **Private to Organization** or **Public**.
![Option to select port visibility in right-click menu](/assets/images/help/codespaces/make-public-option.png)
1. Right-click the port that you want to share, click the **Port Visibility**, then click **Private to Organization** or **Public**.

![Screenshot of the 'Port visibility' option in the right-click menu](/assets/images/help/codespaces/make-public-option.png)

1. To the right of the local address for the port, click the copy icon.
![Copy icon for port URL](/assets/images/help/codespaces/copy-icon-port-url.png)

![Screenshot of the copy icon for the port URL](/assets/images/help/codespaces/copy-icon-port-url.png)

1. Send the copied URL to the person you want to share the port with.

{% data reusables.codespaces.using-tools-to-access-ports-1 %}
{% data reusables.codespaces.find-address-and-token %}
{% data reusables.codespaces.using-tools-to-access-ports-2 %}
{% data reusables.codespaces.port-forwarding-labeling-non-jetbrains %}
{% data reusables.codespaces.port-forwarding-adding-non-jetbrains %}

Expand All @@ -65,21 +75,27 @@ By default, {% data variables.product.prodname_github_codespaces %} forwards por
{% data reusables.codespaces.navigate-to-ports-tab %}
1. Under the list of ports, click **Add port**.

![Add port button](/assets/images/help/codespaces/add-port-button.png)
![Screenshot of the 'Add port' button](/assets/images/help/codespaces/add-port-button.png)

1. Type the port number or address, then press enter.

![Text box to type port button](/assets/images/help/codespaces/port-number-text-box.png)
![Screenshot of entering a port number](/assets/images/help/codespaces/port-number-text-box.png)

{% data reusables.codespaces.port-forwarding-sharing-non-jetbrains %}

{% data reusables.codespaces.navigate-to-ports-tab %}
1. Right click the port that you want to share, select the "Port Visibility" menu, then click **Private to Organization** or **Public**.
![Option to make port public in right-click menu](/assets/images/help/codespaces/make-public-option.png)
1. Right-click the port that you want to share, click **Port Visibility**, then click **Private to Organization** or **Public**.

![Screenshot of the option to make a port public in the right-click menu](/assets/images/help/codespaces/make-public-option.png)

1. To the right of the local address for the port, click the copy icon.
![Copy icon for port URL](/assets/images/help/codespaces/copy-icon-port-url.png)

![Screenshot of the copy icon for the port URL](/assets/images/help/codespaces/copy-icon-port-url.png)

1. Send the copied URL to the person you want to share the port with.

{% data reusables.codespaces.using-tools-to-access-ports-1 %}
{% data reusables.codespaces.find-address-and-token %}
{% data reusables.codespaces.using-tools-to-access-ports-2 %}
{% data reusables.codespaces.port-forwarding-labeling-non-jetbrains %}
{% data reusables.codespaces.port-forwarding-adding-non-jetbrains %}

Expand Down Expand Up @@ -118,6 +134,42 @@ gh codespace ports visibility 80:private 3000:public 3306:org

For more information about this command, see [the {% data variables.product.prodname_cli %} manual](https://cli.github.com/manual/gh_codespace_ports_visibility).

{% data reusables.codespaces.using-tools-to-access-ports-1 %}

### Finding the address to connect to

To find the address for a forwarded port, enter `gh codespace ports`. If you have more than one codespace, select the appropriate codespace from the list that's displayed.

Copy the address and paste it somewhere for later use.

### Finding the GITHUB_TOKEN

1. Start an SSH session for your codespace.

```
gh codespace ssh
```

1. If you have more than one codespace, select the appropriate codespace from the list that's displayed.
1. Display the `GITHUB_TOKEN`.

```
echo $GITHUB_TOKEN
```

The token is a string beginning `ghu_`.

1. Copy the token.

Don't share this access token with anyone.

1. Exit the SSH session.

```
exit
```

{% data reusables.codespaces.using-tools-to-access-ports-2 %}
{% data reusables.codespaces.port-forwarding-labeling-non-jetbrains %}

You can see the port labels when you list the forwarded ports for a codespace. To do this, use the `gh codespace ports` command and then select a codespace.
Expand Down
19 changes: 19 additions & 0 deletions data/reusables/codespaces/find-address-and-token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Finding the address to connect to

1. Open the terminal in your codespace.
1. Click the **PORTS** tab. This lists all of the ports you have forwarded.
1. Right-click the port you want to connect to and click **Copy Local Address**.

![Screenshot of 'Copy Local Address' in the right-click menu](/assets/images/help/codespaces/copy-local-address.png)

1. Paste the copied address somewhere for later use.

### Finding the GITHUB_TOKEN

1. In the terminal in your codespace, enter `echo $GITHUB_TOKEN`.

The token is a string beginning `ghu_`.

1. Copy the token.

Don't share this access token with anyone.
6 changes: 4 additions & 2 deletions data/reusables/codespaces/navigate-to-ports-tab.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
1. In your codespace, under the text editor, click "Ports".
![Ports tab](/assets/images/help/codespaces/ports-tab.png)
1. Open the terminal in your codespace.
1. Click the **PORTS** tab.

![Screenshot of the 'PORTS' tab](/assets/images/help/codespaces/ports-tab.png)
13 changes: 13 additions & 0 deletions data/reusables/codespaces/using-tools-to-access-ports-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Using command-line tools and REST clients to access ports

When you forward a port, your application becomes available at a URL such as `https://USERNAME-CODESPACE-NAME-vrpqrxxrx7x2rxx-4000.preview.app.github.dev`. If you forward a private port from the {% data variables.product.prodname_vscode_shortname %} desktop application, your application will also be available at a localhost port such as `127.0.0.1:4000`.

To access your application using a REST client, such as Postman, or a command-line tool like curl, you don't need to authenticate if you're using a localhost port, or if you're accessing a public port at the remote domain. However, to connect to a private port at the remote domain, you must authenticate by using the `GITHUB_TOKEN` access token in your request.

{% note %}

**Note**: The `GITHUB_TOKEN` is automatically created when you start a codespace and remains the same for the duration of the codespace session. If you stop and then restart a codespace a new `GITHUB_TOKEN` is generated.

{% endnote %}

<!-- Don't delete this comment. It prevents a formatting issue. -->
23 changes: 23 additions & 0 deletions data/reusables/codespaces/using-tools-to-access-ports-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### Using curl to access a forwarded port

In a terminal on your local computer, enter:

```
curl ADDRESS -H "X-Github-Token: TOKEN"
```

Replace `ADDRESS` and `TOKEN` with the values you copied previously.

### Using Postman to access a forwarded port

1. Open Postman.
1. Create a new GET request.
1. Paste the address you copied previously as the request URL.

![Screenshot of the URL pasted into Postman](/assets/images/help/codespaces/postman-screenshot-url.png)

1. In the **Headers** tab, create a new entry where the key is "X-Github-Token" and the value is the `GITHUB_TOKEN` you copied previously.

![Screenshot of the key and token in Postman](/assets/images/help/codespaces/postman-screenshot-key-token.png)

1. Click **Send**.