# End a process

Reports that a copy of your application stopped. The process slot is then free for another copy.

`POST /v1/processes/exit`

## When to exit

Report the exit when your application closes. Send the same process ID as in the spawn.

If your application stops without an exit, for example after a crash, the slot is free when the lease of the process ends. This takes at most the process interval plus the process grace. See [Leases](../../../concepts/concurrent-runs/#leases).

## Validity

An exit does not check the license. It also works on an expired or suspended license. So your application can always free its slot when it closes.

## Effects

- Seatify removes the process. The slot is free immediately, and another copy can spawn.
- An exit does not change the device binding. The device keeps its slot on the license.
- A later spawn with the same process ID registers a new process.
- Seatify records each exit in the activity of the license, a refused one too. See [Activity](../../../concepts/activity/).

## Authentication

Send the publishable key of the product. See [Authentication](../../authentication/).

## Request

| Header | Required | Description |
| --- | --- | --- |
| `Seatify-Publishable-Key` | Yes | The publishable key of the product. |
| `Content-Type` | Yes | Always `application/json`. |
| `Seatify-Nonce` | No | A new random value that the signature includes. See [Signed responses](../../#signed-responses). |
| `Seatify-Signing-Key` | No | The key ID of the signing key in your application. See [Signed responses](../../#signed-responses). |

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `key` | string | Yes | The license key. |
| `device` | object | Yes | The device that the process runs on. |
| `process` | object | Yes | The process to end. |

The `device` object has these fields:

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `fingerprint` | string | Yes | The fingerprint that the device sent in its claim. The fingerprint is case-sensitive. See [Devices](../../../concepts/seats-and-devices/#devices). |
| `hostname` | string | No | The name of the device. An exit does not store the name. So your application can send the same `device` object as in the claim. |

The `process` object has these fields:

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `pid` | string | Yes | The process ID that your application sent in the spawn. The process ID is case-sensitive. |

A `device` object or a `process` object with no values counts as missing.

## Response

The answer is not a license check object. It has only these fields:

| Field | Type | Description |
| --- | --- | --- |
| `exited` | boolean | `true` if Seatify removed the process. |
| `reason` | string | The cause when `exited` is `false`. It is null when `exited` is `true`. |

The answer does not show the state of the license. Validate the license to get it.

## Reasons

A refused exit answers with the status 200, `exited` set to `false` and a reason. The refused exit does not change the license or its processes.

| Reason | Description |
| --- | --- |
| `license_not_found` | No license has this key. |
| `product_mismatch` | The license belongs to another product. |
| `not_claimed` | The device has no binding on this license, or its heartbeat lease has ended. The processes of the device ended with the binding. |
| `process_not_spawned` | The device has no running process with this process ID. The process exited before, or its lease has ended. |

After `not_claimed` or `process_not_spawned`, the process holds no slot. No action is needed.

See [Reasons](../#reasons).

## Errors

| Status | Reason | Description |
| --- | --- | --- |
| 400 | `The request body could not be read.` | The body is not valid JSON, or the process ID is not a string. |
| 400 | `The submitted details are not valid.` | A field is not valid. The `errors` field names it. See the table below. |
| 400 | `The signing key is not known.` | Your organization has no signing key with the ID in `Seatify-Signing-Key`. |
| 400 | `The nonce must be at most 255 characters.` | The value of `Seatify-Nonce` is too long. |
| 401 | `A publishable key is required.` | The request has no `Seatify-Publishable-Key` header, or the header is empty. |
| 401 | `The publishable key is not valid.` | No product has this key. |
| 429 | `There are too many requests. Try again later.` | See [Rate limits](../../#rate-limits). |

These fields can be in `errors`:

| Field | Description |
| --- | --- |
| `key` | The license key is missing or too long. |
| `device` | The request has no device. |
| `device.fingerprint` | The `device` object has no fingerprint, or the fingerprint is too long. |
| `device.hostname` | The hostname is too long. |
| `process` | The request has no process. |
| `process.pid` | The process ID is too long. |

See [Errors](../../#errors).

## Example

{{< tabs >}}
{{< tab name="curl" >}}
```bash
curl https://license.seatify.dev/v1/processes/exit \
  -H "Seatify-Publishable-Key: $SEATIFY_PUBLISHABLE_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "key": "$LICENSE_KEY",
  "device": { "fingerprint": "$DEVICE_FINGERPRINT" },
  "process": { "pid": "$PROCESS_ID" }
}
EOF
```
{{< /tab >}}
{{< tab name="Swift" >}}
```swift
import Foundation

struct ExitResult: Decodable {
    let exited: Bool
    let reason: String?
}

let body: [String: Any] = [
    "key": licenseKey,
    "device": ["fingerprint": fingerprint],
    "process": ["pid": processID],
]

var request = URLRequest(url: URL(string: "https://license.seatify.dev/v1/processes/exit")!)
request.httpMethod = "POST"
request.setValue(publishableKey, forHTTPHeaderField: "Seatify-Publishable-Key")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONSerialization.data(withJSONObject: body)

let (data, _) = try await URLSession.shared.data(for: request)
let result = try JSONDecoder().decode(ExitResult.self, from: data)
```
{{< /tab >}}
{{< tab name="C#" >}}
```csharp
using System.Net.Http.Json;

using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Seatify-Publishable-Key", publishableKey);

var response = await client.PostAsJsonAsync("https://license.seatify.dev/v1/processes/exit", new
{
    key = licenseKey,
    device = new { fingerprint },
    process = new { pid = processId },
});
var result = await response.Content.ReadFromJsonAsync<ExitResult>();

record ExitResult(bool Exited, string? Reason);
```
{{< /tab >}}
{{< tab name="Go" >}}
```go
body, err := json.Marshal(map[string]any{
	"key":     licenseKey,
	"device":  map[string]string{"fingerprint": fingerprint},
	"process": map[string]string{"pid": processID},
})
if err != nil {
	return err
}

req, err := http.NewRequest("POST", "https://license.seatify.dev/v1/processes/exit", bytes.NewReader(body))
if err != nil {
	return err
}
req.Header.Set("Seatify-Publishable-Key", publishableKey)
req.Header.Set("Content-Type", "application/json")

res, err := http.DefaultClient.Do(req)
if err != nil {
	return err
}
defer res.Body.Close()

var result struct {
	Exited bool    `json:"exited"`
	Reason *string `json:"reason"`
}
err = json.NewDecoder(res.Body).Decode(&result)
```
{{< /tab >}}
{{< tab name="JavaScript" >}}
```javascript
const response = await fetch("https://license.seatify.dev/v1/processes/exit", {
  method: "POST",
  headers: {
    "Seatify-Publishable-Key": publishableKey,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    key: licenseKey,
    device: { fingerprint },
    process: { pid: processId },
  }),
});
const result = await response.json();
```
{{< /tab >}}
{{< /tabs >}}

```json
{
  "exited": true,
  "reason": null
}
```

