# Create a service

Source: https://openpanel.sh/docs/api/services/create-a-service

A service runs from a Git repository or a Docker image, on the servers you choose. Public services get addresses on opnl.app and your domains; private ones an address in the organization's private network. Nothing runs until the first deployment.

`POST https://openpanel.sh/api/v1/projects/{projectId}/services`

## Parameters

| Name | In | Required | Description |
|---|---|---|---|
| `projectId` | path | yes | The project's id. |
| `Idempotency-Key` | header | no | Any unique string up to 255 characters. Sending the same request again with it returns the first answer instead of creating twice. |

## Request body

JSON, `ServiceCreate`.

## Responses

| Status | Meaning | Body |
|---|---|---|
| 201 | Create a service | `application/json`, `Service` |
| 401 | No valid credential was sent. | `application/problem+json`, `Problem` |
| 403 | The credential may not do this. | `application/problem+json`, `Problem` |
| 404 | Not found, or not visible to this credential. | `application/problem+json`, `Problem` |
| 409 | It clashes with something that exists, such as a name in use. | `application/problem+json`, `Problem` |
| 422 | The input was refused; detail says why. | `application/problem+json`, `Problem` |
| 429 | Too many requests; wait for Retry-After seconds. | `application/problem+json`, `Problem` |

## Schemas

### HealthCheck

```json
{
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "http"
        },
        "port": {
          "type": "integer"
        },
        "path": {
          "type": "string"
        },
        "intervalSeconds": {
          "type": "integer"
        },
        "timeoutSeconds": {
          "type": "integer"
        },
        "retries": {
          "type": "integer"
        }
      },
      "required": [
        "kind",
        "port",
        "path",
        "intervalSeconds",
        "timeoutSeconds",
        "retries"
      ]
    },
    {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "tcp"
        },
        "port": {
          "type": "integer"
        },
        "intervalSeconds": {
          "type": "integer"
        },
        "timeoutSeconds": {
          "type": "integer"
        },
        "retries": {
          "type": "integer"
        }
      },
      "required": [
        "kind",
        "port",
        "intervalSeconds",
        "timeoutSeconds",
        "retries"
      ]
    }
  ]
}
```

### Port

```json
{
  "type": "object",
  "properties": {
    "protocol": {
      "type": "string",
      "enum": [
        "http",
        "tcp",
        "udp"
      ]
    },
    "port": {
      "type": "integer",
      "description": "The container's port."
    },
    "edgePort": {
      "anyOf": [
        {
          "type": "integer"
        },
        {
          "type": "null"
        }
      ],
      "description": "For a public TCP or UDP port: the port on the edge that reaches it."
    },
    "loopbackPort": {
      "anyOf": [
        {
          "type": "integer"
        },
        {
          "type": "null"
        }
      ],
      "description": "For a private service: its port on the server's 127.0.0.1, which the private network reaches."
    }
  },
  "required": [
    "protocol",
    "port",
    "edgePort",
    "loopbackPort"
  ]
}
```

### Problem

```json
{
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "description": "A link to the error's explanation in the docs."
    },
    "title": {
      "type": "string"
    },
    "status": {
      "type": "integer"
    },
    "code": {
      "type": "string",
      "description": "Stable machine-readable code: not_found, forbidden, invalid, conflict, rate_limited, not_implemented, …"
    },
    "detail": {
      "type": "string",
      "description": "What went wrong, in a sentence written for a person."
    }
  },
  "required": [
    "type",
    "title",
    "status",
    "code",
    "detail"
  ]
}
```

### Service

```json
{
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid"
    },
    "organizationId": {
      "type": "string"
    },
    "projectId": {
      "type": "string",
      "format": "uuid"
    },
    "environmentId": {
      "type": "string",
      "format": "uuid"
    },
    "name": {
      "type": "string"
    },
    "source": {
      "$ref": "#/components/schemas/Source"
    },
    "build": {
      "anyOf": [
        {
          "oneOf": [
            {
              "type": "object",
              "properties": {
                "kind": {
                  "type": "string",
                  "const": "auto"
                }
              },
              "required": [
                "kind"
              ]
            },
            {
              "type": "object",
              "properties": {
                "kind": {
                  "type": "string",
                  "const": "dockerfile"
                },
                "dockerfile": {
                  "type": "string"
                },
                "context": {
                  "type": "string"
                }
              },
              "required": [
                "kind",
                "dockerfile",
                "context"
              ]
            }
          ]
        },
        {
          "type": "null"
        }
      ]
    },
    "startCommand": {
      "type": [
        "string",
        "null"
      ]
    },
    "exposure": {
      "type": "string",
      "enum": [
        "public",
        "private",
        "none"
      ]
    },
    "balancer": {
      "type": "boolean"
    },
    "replicas": {
      "type": "integer"
    },
    "ports": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/Port"
      }
    },
    "healthCheck": {
      "anyOf": [
        {
          "$ref": "#/components/schemas/HealthCheck"
        },
        {
          "type": "null"
        }
      ]
    },
    "resources": {
      "type": "object",
      "properties": {
        "cpus": {
          "type": "number"
        },
        "memoryMb": {
          "type": "integer"
        }
      }
    },
    "serverIds": {
      "type": "array",
      "items": {
        "type": "string",
        "format": "uuid"
      }
    },
    "volumes": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/Volume"
      }
    },
    "deploymentId": {
      "anyOf": [
        {
          "type": "string",
          "format": "uuid"
        },
        {
          "type": "null"
        }
      ],
      "description": "The deployment that runs now."
    },
    "createdBy": {
      "type": [
        "string",
        "null"
      ]
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "updatedAt": {
      "type": "string",
      "format": "date-time"
    }
  },
  "required": [
    "id",
    "organizationId",
    "projectId",
    "environmentId",
    "name",
    "source",
    "build",
    "startCommand",
    "exposure",
    "balancer",
    "replicas",
    "ports",
    "healthCheck",
    "resources",
    "serverIds",
    "volumes",
    "deploymentId",
    "createdBy",
    "createdAt",
    "updatedAt"
  ]
}
```

### ServiceCreate

```json
{
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "description": "A DNS label: up to 32 lowercase letters, digits and hyphens, starting with a letter. Other services in the project reach it by this name."
    },
    "environmentId": {
      "description": "Default the project's production environment.",
      "type": "string",
      "format": "uuid"
    },
    "source": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "git"
            },
            "accountId": {
              "type": "string",
              "format": "uuid",
              "description": "A Git account connected to the organization."
            },
            "repo": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200,
              "description": "owner/name"
            },
            "branch": {
              "description": "Default main.",
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "path": {
              "description": "The folder to build, for a monorepo. Default the repository's root.",
              "type": "string",
              "maxLength": 200
            }
          },
          "required": [
            "kind",
            "accountId",
            "repo"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "image"
            },
            "image": {
              "type": "string",
              "maxLength": 255,
              "description": "A Docker image: nginx, bitnami/redis, ghcr.io/org/app. Docker Hub when no registry is named."
            },
            "tag": {
              "description": "Default latest.",
              "type": "string",
              "maxLength": 128
            }
          },
          "required": [
            "kind",
            "image"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "template"
            },
            "template": {
              "type": "string",
              "maxLength": 64
            },
            "version": {
              "type": "string",
              "maxLength": 64
            }
          },
          "required": [
            "kind",
            "template"
          ],
          "additionalProperties": false
        }
      ],
      "description": "Where the service comes from: a Git repository, a Docker image, or a template."
    },
    "build": {
      "anyOf": [
        {
          "oneOf": [
            {
              "type": "object",
              "properties": {
                "kind": {
                  "type": "string",
                  "const": "auto"
                }
              },
              "required": [
                "kind"
              ],
              "additionalProperties": false
            },
            {
              "type": "object",
              "properties": {
                "kind": {
                  "type": "string",
                  "const": "dockerfile"
                },
                "dockerfile": {
                  "type": "string",
                  "maxLength": 200
                },
                "context": {
                  "type": "string",
                  "maxLength": 200
                }
              },
              "required": [
                "kind"
              ],
              "additionalProperties": false
            }
          ],
          "description": "How a Git source is built: detected automatically, or with a Dockerfile."
        },
        {
          "type": "null"
        }
      ]
    },
    "startCommand": {
      "description": "Run by /bin/sh -c; empty keeps the image's own.",
      "anyOf": [
        {
          "type": "string",
          "maxLength": 2000
        },
        {
          "type": "null"
        }
      ]
    },
    "exposure": {
      "type": "string",
      "enum": [
        "public",
        "private",
        "none"
      ],
      "description": "public: an opnl.app address and your domains; private: an address in the organization's private network; none: no address."
    },
    "balancer": {
      "type": "boolean",
      "description": "The load balancer, for public services: spreads traffic over the replicas, on one or several servers."
    },
    "replicas": {
      "type": "integer",
      "minimum": 1,
      "maximum": 100,
      "description": "How many copies run. More than one needs the load balancer when public; a service with a volume always runs one."
    },
    "ports": {
      "maxItems": 20,
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "protocol": {
            "type": "string",
            "enum": [
              "http",
              "tcp",
              "udp"
            ]
          },
          "port": {
            "type": "integer",
            "minimum": 1,
            "maximum": 65535,
            "description": "The port the container listens on."
          }
        },
        "required": [
          "protocol",
          "port"
        ],
        "additionalProperties": false
      }
    },
    "healthCheck": {
      "anyOf": [
        {
          "oneOf": [
            {
              "type": "object",
              "properties": {
                "kind": {
                  "type": "string",
                  "const": "http"
                },
                "port": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 65535
                },
                "path": {
                  "type": "string",
                  "maxLength": 200,
                  "description": "Starts with /, such as /health."
                },
                "intervalSeconds": {
                  "type": "integer",
                  "minimum": 2,
                  "maximum": 300
                },
                "timeoutSeconds": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 60
                },
                "retries": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 10
                }
              },
              "required": [
                "kind",
                "port",
                "path"
              ],
              "additionalProperties": false
            },
            {
              "type": "object",
              "properties": {
                "kind": {
                  "type": "string",
                  "const": "tcp"
                },
                "port": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 65535
                },
                "intervalSeconds": {
                  "type": "integer",
                  "minimum": 2,
                  "maximum": 300
                },
                "timeoutSeconds": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 60
                },
                "retries": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 10
                }
              },
              "required": [
                "kind",
                "port"
              ],
              "additionalProperties": false
            }
          ],
          "description": "How the agent checks a replica is healthy. The port must be one of the service's ports."
        },
        {
          "type": "null"
        }
      ]
    },
    "resources": {
      "type": "object",
      "properties": {
        "cpus": {
          "type": "number",
          "minimum": 0.1,
          "maximum": 256
        },
        "memoryMb": {
          "type": "integer",
          "minimum": 32,
          "maximum": 1048576
        }
      },
      "additionalProperties": false,
      "description": "Limits per replica."
    },
    "serverIds": {
      "maxItems": 50,
      "type": "array",
      "items": {
        "type": "string",
        "format": "uuid"
      },
      "description": "The servers the service may run on."
    },
    "volumes": {
      "maxItems": 10,
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/VolumeCreate"
      }
    }
  },
  "required": [
    "name",
    "source"
  ],
  "additionalProperties": false
}
```

### Source

```json
{
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "git"
        },
        "accountId": {
          "type": "string",
          "format": "uuid"
        },
        "repo": {
          "type": "string"
        },
        "branch": {
          "type": "string"
        },
        "path": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "accountId",
        "repo",
        "branch",
        "path"
      ]
    },
    {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "image"
        },
        "image": {
          "type": "string"
        },
        "tag": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "image",
        "tag"
      ]
    },
    {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "template"
        },
        "template": {
          "type": "string"
        },
        "version": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "template"
      ]
    }
  ]
}
```

### Volume

```json
{
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid"
    },
    "mountPath": {
      "type": "string"
    },
    "sizeGb": {
      "anyOf": [
        {
          "type": "integer"
        },
        {
          "type": "null"
        }
      ]
    },
    "serverId": {
      "anyOf": [
        {
          "type": "string",
          "format": "uuid"
        },
        {
          "type": "null"
        }
      ],
      "description": "The server its data lives on, set at the first deploy."
    }
  },
  "required": [
    "id",
    "mountPath",
    "sizeGb",
    "serverId"
  ]
}
```

### VolumeCreate

```json
{
  "type": "object",
  "properties": {
    "mountPath": {
      "type": "string",
      "maxLength": 255,
      "description": "An absolute path in the container, such as /data."
    },
    "sizeGb": {
      "anyOf": [
        {
          "type": "integer",
          "minimum": 1,
          "maximum": 100000
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "mountPath"
  ],
  "additionalProperties": false
}
```