> For the complete documentation index, see [llms.txt](https://docs.edgegap.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.edgegap.com/docs/tools-and-integrations/deploy-from-ugs.md).

# Deploy from UGS

Follow this guide if you prefer to keep your Unity Matchmaker and deploy servers to Edgegap.

{% hint style="success" %}
Looking for alternatives? Consider our managed [Matchmaking](/learn/matchmaking.md) with [SDK integration and examples](/unity/matchmaking.md).
{% endhint %}

### Getting Started

This guide focuses on adding Edgegap server hosting as a hosting provider, assuming:

* you have a working Unity Matchmaking setup,
* you have registered on Edgegap and [uploaded your Unity server build](/unity.md).

Please refer to [Unity's Matchmaking documentation](https://docs.unity.com/en-us/matchmaker) for matchmaking topics.

{% hint style="info" %}
See [Switch From Multiplay](/docs/tools-and-integrations/switch-from-multiplay.md) for comparing and mapping Multiplay concepts to Edgegap hosting.
{% endhint %}

[Choosing the right location for your server can reduce latency by up to 58% on average!](/learn/orchestration/deployments.md#server-placement)

In this guide, we'll cover:

* building and deploying `EdgegapAllocator` cloud script module for your Matchmaker,
* retrieving players' public IP addresses used for optimal server placement,
* implementing a webhook listener in UGS cloud script and retrieving server assignment.

### Edgegap Allocator

Start by [downloading the hosting providers repository](https://github.com/Unity-Technologies/matchmaker-hosting-providers/tree/main) with `EdgegapAllocator` source code.

This is [our cloud script](https://github.com/Unity-Technologies/matchmaker-hosting-providers/blob/main/modules/EdgegapAllocator/CONFIGURATION.md) used to start new servers, launched by Unity matchmaker once the match is assembled. This is an official Unity-approved integration.

#### Required Secrets

First, add these secrets in the [Unity Dashboard](https://cloud.unity.com/) under **Administration** > **Secrets**:

* `EDGEGAP_API_TOKEN` - Your Edgegap API token.

Find your token in the [Edgegap Dashboard](https://app.edgegap.com/user-settings?tab=tokens).

#### Required code changes

Edit `Project/EdgegapAllocator.cs` and update these constants:

Replace with your Edgegap application name from the [Applications List](https://app.edgegap.com/application-management/applications/list).

{% code title="ApplicationName (line 34)" %}

```csharp
private const string ApplicationName = "MyApp"; // TODO: Replace with actual application name
```

{% endcode %}

Replace with your Edgegap application's version name you want to use.

{% code title="VersionName (line 35)" %}

```csharp
private const string VersionName = "MyVersion"; // TODO: Replace with actual version name
```

{% endcode %}

Replace with your Edgegap application version's port name that will be used for players to connect.

{% code title="PortName (line 36)" %}

```csharp
private const string PortName = "gameport"; // TODO: Replace with actual port name
```

{% endcode %}

#### Deploy the module

[Follow the official Unity instructions to deploy your customized module using CLI or Editor.](https://github.com/Unity-Technologies/matchmaker-hosting-providers/tree/main#deploy-the-module)

### Retrieve Player IP

{% hint style="warning" %}
**If no player IP address is provided, your deployments fall back to Los Alamos location.**
{% endhint %}

To provide the optimal server location for each match on demand, Edgegap requires you to provide player IP addresses when making a deployment. While Unity does not provide this feature, you can use our C# snippet to implement this in your game clients using free service [ipify](https://ipify.org):

{% code title="PlayerNetworkBehaviour.cs" %}

```csharp
public class PlayerNetworkBehaviour : MonoBehaviour {
    public IEnumerator GetPublicIP(Action<string> callback)
    {
        UnityWebRequest request = UnityWebRequest.Get("https://api.ipify.org?format=json");
        yield return request.SendWebRequest();
    
        if (request.result == UnityWebRequest.Result.Success)
        {
            string responseText = request.downloadHandler.text;
            string clientIp = JObject.Parse(responseText)["ip"].ToString();
            callback(clientIp);
        }
        else
        {
            Debug.LogError("Failed to fetch public IP address.");
        }
        request.Dispose();
    }
}
```

{% endcode %}

Every player is expected to provide their public IP address using custom data in their tickets:

```csharp
CreateTicketOptions ticketOptions = new CreateTicketOptions(
    queueName: "my-queue", 
    attributes: new Dictionary<string, object>
    {
        { "player_ip": "<insert-ip>" },
    }
);
```

### Receiving Assignment

Assignment can be retrieved using the standard Unity matchmaking methods.

Our example will handle assignments for you using [**Status API, rate limited at 20 req/s**](https://docs.edgegap.com/docs/api/dedicated-servers#get-v1-status-deployment_id)**.**

This is sufficient for most games. If you expect more than 50,000 CCU, consider alternative methods.

#### Alternative Assignment

{% hint style="info" %}
For large worldwide launches, a more scalable alternative using [Webhooks](/learn/orchestration/deployments.md#webhooks) is available.
{% endhint %}

Once the match is made, and your cloud script requests a new deployment, your response will not include the connection details yet. Instead, you will receive a form of receipt - a deployment ID.

Your allocator function needs to store this deployment ID in your UGS Game Data alongside the matched players' IDs, so you can look up the players and provide connection details later.

Now, you will need to create another cloud script function `AssignmentProcessor`  with a public URL. This URL must be included in your Allocator's deployment request as `webhook_on_ready` .

Once Edgegap deploys your game server, it will send a webhook HTTP event including all connection details such as FQDN (URL) and the external port for client connections back to your new cloud script. Your `AssignmentProcessor`  will need to look up the deployment ID, and store the connection details alongside it.

Your game clients must poll this game data and attempt connection as soon as the FQDN and external port are retrieved.

<figure><img src="/files/Y9q9Ssfv1aVTsOiCoXlI" alt=""><figcaption></figcaption></figure>
