# Create Agent

POST https://services.leadconnectorhq.com/agent-studio/agent

Creates a new agent with staging version. The agent will be created with an initial staging version that can later be promoted to production. 

## Authentication

Send `Authorization: Bearer <token>` using:

- **Sub-Account token: OAuth access token or Private Integration Token.** Scopes: `agent-studio.write`

## Parameters

### Header Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `Version` | string | Yes | API Version |

### Query Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `source` | string | No | Origin channel attributed to this write for auditing (e.g. public_api, internal) |

## OpenAPI Specification

```json
{
  "openapi": "3.0.0",
  "info": {
    "title": "LeadConnector API",
    "version": "v3"
  },
  "servers": [
    {
      "url": "https://services.leadconnectorhq.com"
    }
  ],
  "paths": {
    "/agent-studio/agent": {
      "post": {
        "summary": "Create Agent",
        "operationId": "createAgent",
        "description": "Creates a new agent with staging version. The agent will be created with an initial staging version that can later be promoted to production. ",
        "parameters": [
          {
            "name": "Version",
            "in": "header",
            "description": "API Version",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "v3"
              ]
            }
          },
          {
            "name": "source",
            "required": false,
            "in": "query",
            "schema": {
              "type": "string"
            },
            "example": "api",
            "description": "Origin channel attributed to this write for auditing (e.g. public_api, internal)"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreatePublicAgentDTO"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Agent created successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreatePublicAgentResponseDTO"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BadRequestDTO"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UnauthorizedDTO"
                }
              }
            }
          },
          "422": {
            "description": "Unprocessable Entity",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UnprocessableDTO"
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/InternalServerErrorDTO"
                }
              }
            }
          }
        },
        "security": [
          {
            "Location-Access": [
              "agent-studio.write"
            ]
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearer": {
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "name": "Authorization",
        "in": "header",
        "description": "Use the Access Token generated with user type as Sub-Account (OR) Private Integration Token of Sub-Account.",
        "type": "http"
      },
      "Location-Access": {
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "name": "Authorization",
        "in": "header",
        "description": "Use the Access Token generated with user type as Sub-Account (OR) Private Integration Token of Sub-Account.",
        "type": "http"
      },
      "Location-Access-Only": {
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "name": "Authorization",
        "in": "header",
        "description": "Use the Access Token generated with user type as Sub-Account.",
        "type": "http"
      },
      "Agency-Access": {
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "name": "Authorization",
        "in": "header",
        "description": "Use the Access Token generated with user type as Agency (OR) Private Integration Token of Agency.",
        "type": "http"
      },
      "Agency-Access-Only": {
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "name": "Authorization",
        "in": "header",
        "description": "Use the Access Token generated with user type as Agency.",
        "type": "http"
      }
    },
    "schemas": {
      "CreatePublicAgentDTO": {
        "type": "object",
        "properties": {
          "locationId": {
            "type": "string",
            "description": "Location ID",
            "example": "C2QujeCh8ZnC7al2InWR"
          },
          "name": {
            "type": "string",
            "description": "Name of the agent",
            "example": "Customer Support Agent"
          },
          "description": {
            "type": "string",
            "description": "Description of the agent",
            "example": "AI agent specialized in handling customer inquiries and support tickets"
          },
          "agencyId": {
            "type": "string",
            "description": "Agency ID",
            "example": "gjL2sFNXJfJYa3d2OYSN"
          },
          "authorId": {
            "type": "string",
            "description": "Author ID",
            "example": "usr_abc123def456"
          },
          "authorName": {
            "type": "string",
            "description": "Author name",
            "example": "John Doe"
          },
          "authorEmail": {
            "type": "string",
            "description": "Author email",
            "example": "john@example.com"
          },
          "status": {
            "type": "string",
            "description": "Status of the agent",
            "enum": [
              "active",
              "inactive",
              "archived"
            ],
            "example": "active"
          },
          "version": {
            "type": "object",
            "description": "Version data for the agent including nodes, edges, and configuration",
            "example": {
              "versionName": "Version 1",
              "description": "Initial version",
              "nodes": [],
              "edges": [],
              "uiNodes": [],
              "uiEdges": [],
              "globalVariables": [],
              "inputVariables": [],
              "runtimeVariables": [],
              "scopes": []
            }
          },
          "nodes": {
            "description": "Nodes array (deprecated, prefer using version.nodes)",
            "example": [],
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "edges": {
            "description": "Edges array (deprecated, prefer using version.edges)",
            "example": [],
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        },
        "required": [
          "locationId",
          "status",
          "version"
        ],
        "title": "CreatePublicAgentDTO"
      },
      "CreatePublicAgentResponseDTO": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "description": "Success status",
            "example": true
          },
          "message": {
            "type": "string",
            "description": "Response message",
            "example": "Agent created successfully with staging version."
          },
          "agent": {
            "type": "object",
            "description": "Created agent data with metadata",
            "example": {
              "agentId": "p1q2r3s4t5u6v7w8x9y0z1a2",
              "name": "Customer Support Agent",
              "description": "AI agent specialized in handling customer inquiries and support tickets",
              "locationId": "C2QujeCh8ZnC7al2InWR",
              "agencyId": "gjL2sFNXJfJYa3d2OYSN",
              "status": "active",
              "authorId": "usr_abc123def456",
              "folderId": "C2QujeCh8ZnC7al2InWR",
              "folderName": null,
              "createdAt": "2024-02-27T10:30:00.000Z",
              "updatedAt": "2024-02-27T10:30:00.000Z"
            }
          },
          "versions": {
            "type": "array",
            "description": "Created versions array (initial staging version)",
            "example": [
              {
                "versionId": "v1a2b3c4d5e6f7g8h9i0",
                "agentId": "p1q2r3s4t5u6v7w8x9y0z1a2",
                "versionName": "Customer Support Agent v1",
                "state": "staging",
                "isPublished": false,
                "version": 1,
                "createdAt": "2024-02-27T10:30:00.000Z"
              }
            ]
          }
        },
        "required": [
          "success",
          "message",
          "agent",
          "versions"
        ],
        "title": "CreatePublicAgentResponseDTO"
      },
      "BadRequestDTO": {
        "type": "object",
        "properties": {
          "statusCode": {
            "type": "number",
            "example": 400
          },
          "message": {
            "type": "string",
            "example": "Bad Request"
          }
        },
        "title": "BadRequestDTO"
      },
      "UnauthorizedDTO": {
        "type": "object",
        "properties": {
          "statusCode": {
            "type": "number",
            "example": 401
          },
          "message": {
            "type": "string",
            "example": "Invalid token: access token is invalid"
          },
          "error": {
            "type": "string",
            "example": "Unauthorized"
          }
        },
        "title": "UnauthorizedDTO"
      },
      "UnprocessableDTO": {
        "type": "object",
        "properties": {
          "statusCode": {
            "type": "number",
            "example": 422
          },
          "message": {
            "example": [
              "Unprocessable Entity"
            ],
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "error": {
            "type": "string",
            "example": "Unprocessable Entity"
          }
        },
        "title": "UnprocessableDTO"
      },
      "InternalServerErrorDTO": {
        "type": "object",
        "properties": {
          "statusCode": {
            "type": "number",
            "example": 500,
            "description": "HTTP status code"
          },
          "message": {
            "type": "string",
            "example": "Internal Server Error",
            "description": "Error message"
          }
        },
        "title": "InternalServerErrorDTO"
      }
    }
  }
}
```

## Code Examples

**cURL**

```bash
curl -L -X POST 'https://services.leadconnectorhq.com/agent-studio/agent' \
  -H 'Authorization: Bearer <TOKEN>' \
  -H 'Version: v3' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "locationId": "C2QujeCh8ZnC7al2InWR",
  "name": "Customer Support Agent",
  "description": "AI agent specialized in handling customer inquiries and support tickets",
  "agencyId": "gjL2sFNXJfJYa3d2OYSN",
  "authorId": "usr_abc123def456",
  "authorName": "John Doe",
  "authorEmail": "john@example.com",
  "status": "active",
  "version": {
    "versionName": "Version 1",
    "description": "Initial version",
    "nodes": [],
    "edges": [],
    "uiNodes": [],
    "uiEdges": [],
    "globalVariables": [],
    "inputVariables": [],
    "runtimeVariables": [],
    "scopes": []
  },
  "nodes": [],
  "edges": []
}'
```

**Node.js**

```javascript
const options = {
  method: 'POST',
  headers: {
    Authorization: 'Bearer <TOKEN>',
    Version: 'v3',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
      "locationId": "C2QujeCh8ZnC7al2InWR",
      "name": "Customer Support Agent",
      "description": "AI agent specialized in handling customer inquiries and support tickets",
      "agencyId": "gjL2sFNXJfJYa3d2OYSN",
      "authorId": "usr_abc123def456",
      "authorName": "John Doe",
      "authorEmail": "john@example.com",
      "status": "active",
      "version": {
        "versionName": "Version 1",
        "description": "Initial version",
        "nodes": [],
        "edges": [],
        "uiNodes": [],
        "uiEdges": [],
        "globalVariables": [],
        "inputVariables": [],
        "runtimeVariables": [],
        "scopes": []
      },
      "nodes": [],
      "edges": []
  })
};

try {
  const response = await fetch('https://services.leadconnectorhq.com/agent-studio/agent', options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

**Python**

```python
import requests

url = "https://services.leadconnectorhq.com/agent-studio/agent"

headers = {
    "Authorization": "Bearer <TOKEN>",
    "Version": "v3"
}

payload = {
    "locationId": "C2QujeCh8ZnC7al2InWR",
    "name": "Customer Support Agent",
    "description": "AI agent specialized in handling customer inquiries and support tickets",
    "agencyId": "gjL2sFNXJfJYa3d2OYSN",
    "authorId": "usr_abc123def456",
    "authorName": "John Doe",
    "authorEmail": "john@example.com",
    "status": "active",
    "version": {
        "versionName": "Version 1",
        "description": "Initial version",
        "nodes": [],
        "edges": [],
        "uiNodes": [],
        "uiEdges": [],
        "globalVariables": [],
        "inputVariables": [],
        "runtimeVariables": [],
        "scopes": []
    },
    "nodes": [],
    "edges": []
}

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

**PHP**

```php
<?php
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => 'https://services.leadconnectorhq.com/agent-studio/agent',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_HTTPHEADER => [
    'Authorization: Bearer <TOKEN>',
    'Version: v3',
    'Content-Type: application/json',
  ],
  CURLOPT_POSTFIELDS => '{
  "locationId": "C2QujeCh8ZnC7al2InWR",
  "name": "Customer Support Agent",
  "description": "AI agent specialized in handling customer inquiries and support tickets",
  "agencyId": "gjL2sFNXJfJYa3d2OYSN",
  "authorId": "usr_abc123def456",
  "authorName": "John Doe",
  "authorEmail": "john@example.com",
  "status": "active",
  "version": {
    "versionName": "Version 1",
    "description": "Initial version",
    "nodes": [],
    "edges": [],
    "uiNodes": [],
    "uiEdges": [],
    "globalVariables": [],
    "inputVariables": [],
    "runtimeVariables": [],
    "scopes": []
  },
  "nodes": [],
  "edges": []
}',
]);

$response = curl_exec($curl);
curl_close($curl);
echo $response;
```

**Java**

```java
HttpResponse<String> response = Unirest
  .post("https://services.leadconnectorhq.com/agent-studio/agent")
  .header("Authorization", "Bearer <TOKEN>")
  .header("Version", "v3")
  .header("Content-Type", "application/json")
  .body("{\"locationId\":\"C2QujeCh8ZnC7al2InWR\",\"name\":\"Customer Support Agent\",\"description\":\"AI agent specialized in handling customer inquiries and support tickets\",\"agencyId\":\"gjL2sFNXJfJYa3d2OYSN\",\"authorId\":\"usr_abc123def456\",\"authorName\":\"John Doe\",\"authorEmail\":\"john@example.com\",\"status\":\"active\",\"version\":{\"versionName\":\"Version 1\",\"description\":\"Initial version\",\"nodes\":[],\"edges\":[],\"uiNodes\":[],\"uiEdges\":[],\"globalVariables\":[],\"inputVariables\":[],\"runtimeVariables\":[],\"scopes\":[]},\"nodes\":[],\"edges\":[]}")
  .asString();
```

**Go**

```go
package main

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

func main() {
  url := "https://services.leadconnectorhq.com/agent-studio/agent"
  payload := strings.NewReader(`{
  "locationId": "C2QujeCh8ZnC7al2InWR",
  "name": "Customer Support Agent",
  "description": "AI agent specialized in handling customer inquiries and support tickets",
  "agencyId": "gjL2sFNXJfJYa3d2OYSN",
  "authorId": "usr_abc123def456",
  "authorName": "John Doe",
  "authorEmail": "john@example.com",
  "status": "active",
  "version": {
    "versionName": "Version 1",
    "description": "Initial version",
    "nodes": [],
    "edges": [],
    "uiNodes": [],
    "uiEdges": [],
    "globalVariables": [],
    "inputVariables": [],
    "runtimeVariables": [],
    "scopes": []
  },
  "nodes": [],
  "edges": []
}`)
  req, _ := http.NewRequest("POST", url, payload)
  req.Header.Add("Authorization", "Bearer <TOKEN>")
  req.Header.Add("Version", "v3")

  res, _ := http.DefaultClient.Do(req)
  defer res.Body.Close()
  body, _ := io.ReadAll(res.Body)
  fmt.Println(string(body))
}
```

**Ruby**

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

url = URI("https://services.leadconnectorhq.com/agent-studio/agent")

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

request = Net::HTTP::Post.new(url)
request["Authorization"] = "Bearer <TOKEN>"
request["Version"] = "v3"
request["Content-Type"] = "application/json"
request.body = '{
  "locationId": "C2QujeCh8ZnC7al2InWR",
  "name": "Customer Support Agent",
  "description": "AI agent specialized in handling customer inquiries and support tickets",
  "agencyId": "gjL2sFNXJfJYa3d2OYSN",
  "authorId": "usr_abc123def456",
  "authorName": "John Doe",
  "authorEmail": "john@example.com",
  "status": "active",
  "version": {
    "versionName": "Version 1",
    "description": "Initial version",
    "nodes": [],
    "edges": [],
    "uiNodes": [],
    "uiEdges": [],
    "globalVariables": [],
    "inputVariables": [],
    "runtimeVariables": [],
    "scopes": []
  },
  "nodes": [],
  "edges": []
}'

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

**PowerShell**

```powershell
$headers = @{
  "Authorization" = "Bearer <TOKEN>"
  "Version" = "v3"
}

$body = '{
  "locationId": "C2QujeCh8ZnC7al2InWR",
  "name": "Customer Support Agent",
  "description": "AI agent specialized in handling customer inquiries and support tickets",
  "agencyId": "gjL2sFNXJfJYa3d2OYSN",
  "authorId": "usr_abc123def456",
  "authorName": "John Doe",
  "authorEmail": "john@example.com",
  "status": "active",
  "version": {
    "versionName": "Version 1",
    "description": "Initial version",
    "nodes": [],
    "edges": [],
    "uiNodes": [],
    "uiEdges": [],
    "globalVariables": [],
    "inputVariables": [],
    "runtimeVariables": [],
    "scopes": []
  },
  "nodes": [],
  "edges": []
}'

$response = Invoke-RestMethod -Uri 'https://services.leadconnectorhq.com/agent-studio/agent' -Method 'POST' -Headers $headers -Body $body -ContentType 'application/json'
$response | ConvertTo-Json
```
