# Create layer

POST https://app.launchdarkly.com/api/v2/projects/{projectKey}/layers
Content-Type: application/json

Create a layer. Experiments running in the same layer are granted mutually-exclusive traffic.


Reference: https://launchdarkly.com/docs/api/layers/create-layer

## OpenAPI Specification

```yaml
openapi: 3.1.0
info:
  title: LaunchDarkly REST API
  version: 1.0.0
paths:
  /api/v2/projects/{projectKey}/layers:
    post:
      operationId: create-layer
      summary: Create layer
      description: >
        Create a layer. Experiments running in the same layer are granted
        mutually-exclusive traffic.
      tags:
        - subpackage_layers
      parameters:
        - name: projectKey
          in: path
          description: The project key
          required: true
          schema:
            type: string
            format: string
        - name: Authorization
          in: header
          required: true
          schema:
            type: string
      responses:
        '201':
          description: Layer response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LayerRep'
        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InvalidRequestErrorRep'
        '403':
          description: Forbidden
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ForbiddenErrorRep'
        '404':
          description: Invalid resource identifier
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/NotFoundErrorRep'
        '429':
          description: Rate limited
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RateLimitedErrorRep'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/LayerPost'
servers:
  - url: https://app.launchdarkly.com
  - url: https://app.launchdarkly.us
components:
  schemas:
    LayerPost:
      type: object
      properties:
        key:
          type: string
          description: Unique identifier for the layer
        name:
          type: string
          description: Layer name
        description:
          type: string
          description: The checkout flow for the application
      required:
        - key
        - name
        - description
      title: LayerPost
    UnixMillis:
      type: integer
      format: int64
      title: UnixMillis
    LayerReservationRep:
      type: object
      properties:
        experimentKey:
          type: string
          description: The key of the experiment
        flagKey:
          type: string
          description: The key of the flag
        reservationPercent:
          type: integer
          description: The percentage of traffic reserved for the experiment
      required:
        - experimentKey
        - flagKey
        - reservationPercent
      title: LayerReservationRep
    LayerConfigurationRep:
      type: object
      properties:
        reservations:
          type: array
          items:
            $ref: '#/components/schemas/LayerReservationRep'
          description: The experiment reservations for the layer
      required:
        - reservations
      title: LayerConfigurationRep
    LayerRep:
      type: object
      properties:
        key:
          type: string
          description: The key of the layer
        name:
          type: string
          description: The name of the layer
        description:
          type: string
          description: The description of the layer
        createdAt:
          $ref: '#/components/schemas/UnixMillis'
          description: The date and time when the layer was created
        randomizationUnit:
          type: string
          description: The unit of randomization for the layer
        environments:
          type: object
          additionalProperties:
            $ref: '#/components/schemas/LayerConfigurationRep'
          description: The layer configurations for each requested environment
      required:
        - key
        - name
        - description
        - createdAt
      title: LayerRep
    InvalidRequestErrorRep:
      type: object
      properties:
        code:
          type: string
          description: Specific error code encountered
        message:
          type: string
          description: Description of the error
      required:
        - code
        - message
      title: InvalidRequestErrorRep
    ForbiddenErrorRep:
      type: object
      properties:
        code:
          type: string
          description: Specific error code encountered
        message:
          type: string
          description: Description of the error
      required:
        - code
        - message
      title: ForbiddenErrorRep
    NotFoundErrorRep:
      type: object
      properties:
        code:
          type: string
          description: Specific error code encountered
        message:
          type: string
          description: Description of the error
      required:
        - code
        - message
      title: NotFoundErrorRep
    RateLimitedErrorRep:
      type: object
      properties:
        code:
          type: string
          description: Specific error code encountered
        message:
          type: string
          description: Description of the error
      required:
        - code
        - message
      title: RateLimitedErrorRep
  securitySchemes:
    ApiKey:
      type: apiKey
      in: header
      name: Authorization

```

## SDK Code Examples

```python
import requests

url = "https://app.launchdarkly.com/api/v2/projects/projectKey/layers"

payload = {
    "key": "checkout-flow",
    "name": "Checkout Flow",
    "description": "string"
}
headers = {
    "Authorization": "<apiKey>",
    "Content-Type": "application/json"
}

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

print(response.json())
```

```javascript
const url = 'https://app.launchdarkly.com/api/v2/projects/projectKey/layers';
const options = {
  method: 'POST',
  headers: {Authorization: '<apiKey>', 'Content-Type': 'application/json'},
  body: '{"key":"checkout-flow","name":"Checkout Flow","description":"string"}'
};

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://app.launchdarkly.com/api/v2/projects/projectKey/layers"

	payload := strings.NewReader("{\n  \"key\": \"checkout-flow\",\n  \"name\": \"Checkout Flow\",\n  \"description\": \"string\"\n}")

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

	req.Header.Add("Authorization", "<apiKey>")
	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://app.launchdarkly.com/api/v2/projects/projectKey/layers")

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

request = Net::HTTP::Post.new(url)
request["Authorization"] = '<apiKey>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"key\": \"checkout-flow\",\n  \"name\": \"Checkout Flow\",\n  \"description\": \"string\"\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://app.launchdarkly.com/api/v2/projects/projectKey/layers")
  .header("Authorization", "<apiKey>")
  .header("Content-Type", "application/json")
  .body("{\n  \"key\": \"checkout-flow\",\n  \"name\": \"Checkout Flow\",\n  \"description\": \"string\"\n}")
  .asString();
```

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

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://app.launchdarkly.com/api/v2/projects/projectKey/layers', [
  'body' => '{
  "key": "checkout-flow",
  "name": "Checkout Flow",
  "description": "string"
}',
  'headers' => [
    'Authorization' => '<apiKey>',
    'Content-Type' => 'application/json',
  ],
]);

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

```csharp
using RestSharp;

var client = new RestClient("https://app.launchdarkly.com/api/v2/projects/projectKey/layers");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "<apiKey>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"key\": \"checkout-flow\",\n  \"name\": \"Checkout Flow\",\n  \"description\": \"string\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = [
  "Authorization": "<apiKey>",
  "Content-Type": "application/json"
]
let parameters = [
  "key": "checkout-flow",
  "name": "Checkout Flow",
  "description": "string"
] as [String : Any]

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

let request = NSMutableURLRequest(url: NSURL(string: "https://app.launchdarkly.com/api/v2/projects/projectKey/layers")! 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()
```