> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://en.megaport.ferndocs.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://en.megaport.ferndocs.com/_mcp/server.

# Generate Service Key

POST https://api.megaport.com/v2/service/key
Content-Type: application/json

Creates a service key.

Before you can create a service key, you need a service and you specify the ID of the service in the `productUid` field of the request.

Customize the Body of the request with the configuration details for the service key.

`active` determines whether the service key is available for use. Valid values are `true` if you want the key to be available right away and `false` if you donâ€™t want the key to be available right away. The service key can be updated at a later time to change this value.

`singleUse` determines whether to create a single-use or multi-use service key. Valid values are `true` (single-use) and `false` (multi-use). With a multi-use key, the customer that you share the key with can request multiple connections using that key. For single-use keys only, specify a VLAN ID (`vlan`).

`maxSpeed` is the maximum rate limit for the service key in Mbps. This must be equal to or lower than 1000. Leaving this value blank will limit the service key to the maximum available service speed.

`validFor` is the range of dates for which the service key is valid. The dates are entered in UNIX timestamp date format. You can use tools such as [https://www.epochconverter.com/](https://www.epochconverter.com/) or [https://it-tools.tech/date-converter](https://it-tools.tech/date-converter) to determine the correct value to enter.

For more information, see [Creating a Service Key with the API](https://docs.megaport.com/api/api-create-servicekey/).

Reference: https://en.megaport.ferndocs.com/api-reference/connections/generate-service-key

## Servers

- `https://api.megaport.com` (Production environment, default)
- `https://api-staging.megaport.com` (Staging environment (test only, reset every 24 hours))

## Request

### Body (application/json)

This endpoint expects an object.

- `_set_singleUse_to_false_to_make_service_key_multiUse` (boolean, optional)
- `active` (boolean, optional)
- `description` (string, optional)
- `maxSpeed` (string, optional)
- `preApproved` (boolean, optional)
- `productUid` (string, optional)
- `singleUse` (boolean, optional)
- `validFor` (V2ServiceKeyPostRequestBodyContentApplicationJsonSchemaValidFor, optional)
- `vlan` (double, optional)

## Response

### 200

Generate Service Key / Generate Service Key

- `data` (V2ServiceKeyPostResponsesContentApplicationJsonSchemaData, optional)
- `message` (string, optional)
- `terms` (string, optional)

## Types

### V2ServiceKeyPostRequestBodyContentApplicationJsonSchemaValidFor

- `_make_end_timestamp_equal_to_1_month_forward` (double, optional)
- `_make_start_timestamp_equal_to_now` (double, optional)
- `end` (double, optional)
- `start` (double, optional)

### V2ServiceKeyPostResponsesContentApplicationJsonSchemaData

- `active` (boolean, optional)
- `bEnd` (V2ServiceKeyPostResponsesContentApplicationJsonSchemaDataBEnd, optional)
- `companyId` (double, optional)
- `companyName` (string, optional)
- `companyUid` (string, optional)
- `createDate` (double, optional)
- `description` (string, optional)
- `expired` (boolean, optional)
- `key` (string, optional)
- `lastUsed` (any, optional, nullable)
- `maxSpeed` (double, optional)
- `preApproved` (boolean, optional)
- `productDto` (V2ServiceKeyPostResponsesContentApplicationJsonSchemaDataProductDto, optional)
- `productId` (double, optional)
- `productName` (string, optional)
- `productUid` (string, optional)
- `singleUse` (boolean, optional)
- `valid` (boolean, optional)
- `validFor` (V2ServiceKeyPostResponsesContentApplicationJsonSchemaDataValidFor, optional)
- `vlan` (double, optional)

### V2ServiceKeyPostResponsesContentApplicationJsonSchemaDataBEnd

### V2ServiceKeyPostResponsesContentApplicationJsonSchemaDataProductDto

- `companyName` (string, optional)
- `companyUid` (string, optional)
- `connectType` (string, optional)
- `diversityZone` (string, optional)
- `locationId` (double, optional)
- `portSpeed` (double, optional)
- `productName` (string, optional)
- `productUid` (string, optional)
- `vxcPermitted` (boolean, optional)

### V2ServiceKeyPostResponsesContentApplicationJsonSchemaDataValidFor

- `end` (double, optional)
- `start` (double, optional)

## Examples

**Request**

```json
{
  "_set_singleUse_to_false_to_make_service_key_multiUse": true,
  "active": true,
  "description": "Documentation Service Key - Single Use",
  "maxSpeed": "500",
  "preApproved": true,
  "productUid": "73b2a452-9f93-4c45-afa0-887cf7f17e89",
  "singleUse": true,
  "validFor": {
    "_make_end_timestamp_equal_to_1_month_forward": 1612015200000,
    "_make_start_timestamp_equal_to_now": 1608506197135,
    "end": 1612015200000,
    "start": 1608506197135
  },
  "vlan": 3
}
```

**Response**

```json
{
  "data": {
    "active": true,
    "bEnd": {},
    "companyId": 1153,
    "companyName": "Megaport Lab",
    "companyUid": "160208ae-01e4-4cb9-8d57-03a197bd47a8",
    "createDate": 1738039556254,
    "description": "Documentation Service Key - Single Use",
    "expired": true,
    "key": "96cbcd3b-024e-4ea2-9b9f-ebfe5296b979",
    "lastUsed": null,
    "maxSpeed": 500,
    "preApproved": true,
    "productDto": {
      "companyName": "Megaport Lab",
      "companyUid": "160208ae-01e4-4cb9-8d57-03a197bd47a8",
      "connectType": "DEFAULT",
      "diversityZone": "blue",
      "locationId": 68,
      "portSpeed": 10000,
      "productName": "Example Port",
      "productUid": "73b2a452-9f93-4c45-afa0-887cf7f17e89",
      "vxcPermitted": true
    },
    "productId": 226855,
    "productName": "Example Port",
    "productUid": "73b2a452-9f93-4c45-afa0-887cf7f17e89",
    "singleUse": true,
    "valid": false,
    "validFor": {
      "end": 1612015200000,
      "start": 1608506197135
    },
    "vlan": 3
  },
  "message": "New service key [96cbcd3b-024e-4ea2-9b9f-ebfe5296b979] generated",
  "terms": "This data is subject to the Acceptable Use Policy https://www.megaport.com/legal/acceptable-use-policy"
}
```

**SDK Code**

```python
import requests

url = "https://api.megaport.com/v2/service/key"

payload = {
    "_set_singleUse_to_false_to_make_service_key_multiUse": True,
    "active": True,
    "description": "Documentation Service Key - Single Use",
    "maxSpeed": "500",
    "preApproved": True,
    "productUid": "73b2a452-9f93-4c45-afa0-887cf7f17e89",
    "singleUse": True,
    "validFor": {
        "_make_end_timestamp_equal_to_1_month_forward": 1612015200000,
        "_make_start_timestamp_equal_to_now": 1608506197135,
        "end": 1612015200000,
        "start": 1608506197135
    },
    "vlan": 3
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript
const url = 'https://api.megaport.com/v2/service/key';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: '{"_set_singleUse_to_false_to_make_service_key_multiUse":true,"active":true,"description":"Documentation Service Key - Single Use","maxSpeed":"500","preApproved":true,"productUid":"73b2a452-9f93-4c45-afa0-887cf7f17e89","singleUse":true,"validFor":{"_make_end_timestamp_equal_to_1_month_forward":1612015200000,"_make_start_timestamp_equal_to_now":1608506197135,"end":1612015200000,"start":1608506197135},"vlan":3}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.megaport.com/v2/service/key"

	payload := strings.NewReader("{\n  \"_set_singleUse_to_false_to_make_service_key_multiUse\": true,\n  \"active\": true,\n  \"description\": \"Documentation Service Key - Single Use\",\n  \"maxSpeed\": \"500\",\n  \"preApproved\": true,\n  \"productUid\": \"73b2a452-9f93-4c45-afa0-887cf7f17e89\",\n  \"singleUse\": true,\n  \"validFor\": {\n    \"_make_end_timestamp_equal_to_1_month_forward\": 1612015200000,\n    \"_make_start_timestamp_equal_to_now\": 1608506197135,\n    \"end\": 1612015200000,\n    \"start\": 1608506197135\n  },\n  \"vlan\": 3\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://api.megaport.com/v2/service/key")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n  \"_set_singleUse_to_false_to_make_service_key_multiUse\": true,\n  \"active\": true,\n  \"description\": \"Documentation Service Key - Single Use\",\n  \"maxSpeed\": \"500\",\n  \"preApproved\": true,\n  \"productUid\": \"73b2a452-9f93-4c45-afa0-887cf7f17e89\",\n  \"singleUse\": true,\n  \"validFor\": {\n    \"_make_end_timestamp_equal_to_1_month_forward\": 1612015200000,\n    \"_make_start_timestamp_equal_to_now\": 1608506197135,\n    \"end\": 1612015200000,\n    \"start\": 1608506197135\n  },\n  \"vlan\": 3\n}"

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.megaport.com/v2/service/key")
  .header("Content-Type", "application/json")
  .body("{\n  \"_set_singleUse_to_false_to_make_service_key_multiUse\": true,\n  \"active\": true,\n  \"description\": \"Documentation Service Key - Single Use\",\n  \"maxSpeed\": \"500\",\n  \"preApproved\": true,\n  \"productUid\": \"73b2a452-9f93-4c45-afa0-887cf7f17e89\",\n  \"singleUse\": true,\n  \"validFor\": {\n    \"_make_end_timestamp_equal_to_1_month_forward\": 1612015200000,\n    \"_make_start_timestamp_equal_to_now\": 1608506197135,\n    \"end\": 1612015200000,\n    \"start\": 1608506197135\n  },\n  \"vlan\": 3\n}")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.megaport.com/v2/service/key', [
  'body' => '{
  "_set_singleUse_to_false_to_make_service_key_multiUse": true,
  "active": true,
  "description": "Documentation Service Key - Single Use",
  "maxSpeed": "500",
  "preApproved": true,
  "productUid": "73b2a452-9f93-4c45-afa0-887cf7f17e89",
  "singleUse": true,
  "validFor": {
    "_make_end_timestamp_equal_to_1_month_forward": 1612015200000,
    "_make_start_timestamp_equal_to_now": 1608506197135,
    "end": 1612015200000,
    "start": 1608506197135
  },
  "vlan": 3
}',
  'headers' => [
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://api.megaport.com/v2/service/key");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"_set_singleUse_to_false_to_make_service_key_multiUse\": true,\n  \"active\": true,\n  \"description\": \"Documentation Service Key - Single Use\",\n  \"maxSpeed\": \"500\",\n  \"preApproved\": true,\n  \"productUid\": \"73b2a452-9f93-4c45-afa0-887cf7f17e89\",\n  \"singleUse\": true,\n  \"validFor\": {\n    \"_make_end_timestamp_equal_to_1_month_forward\": 1612015200000,\n    \"_make_start_timestamp_equal_to_now\": 1608506197135,\n    \"end\": 1612015200000,\n    \"start\": 1608506197135\n  },\n  \"vlan\": 3\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = [
  "_set_singleUse_to_false_to_make_service_key_multiUse": true,
  "active": true,
  "description": "Documentation Service Key - Single Use",
  "maxSpeed": "500",
  "preApproved": true,
  "productUid": "73b2a452-9f93-4c45-afa0-887cf7f17e89",
  "singleUse": true,
  "validFor": [
    "_make_end_timestamp_equal_to_1_month_forward": 1612015200000,
    "_make_start_timestamp_equal_to_now": 1608506197135,
    "end": 1612015200000,
    "start": 1608506197135
  ],
  "vlan": 3
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.megaport.com/v2/service/key")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```