# Take an action lock — API reference

> Takes the lock for one handler run so that only one connected machine acts, or names the machine that holds it.

Source: https://upfour.io/docs/api/runner/take-an-action-lock

[Developers](https://upfour.io/docs/api.md)/[Event stream (up4 CLI)](https://upfour.io/docs/api/runner.md)

Takes the lock for one handler run so that only one connected machine acts, or names the machine that holds it.

POST https://app.upfour.io/v1/runner/locks

scope runner.report.write stable [Idempotency-Key supported](https://upfour.io/docs/api/idempotency.md) returns 200

## Parameters

| Field | Type | Description |
| --- | --- | --- |
| `X-Up4-Runner-ID` required | uuid | header parameter · Stable UUID of the runner. |
| `X-Up4-Runner-Name` required | string length 1–64 | header parameter · Printable runner name. |

## Body

`application/json` optional 7 fields

event_idstring (required)

The stream event that triggers the action.

handlerstring (required)

The handler name from the CLI configuration.

hold_msinteger (required)

How long the lock holds without an end: the handler timeout plus a margin. At most one day.

keystring (required)

Opaque lock key, 1 to 128 printable characters. The CLI derives it from the lock group, the client and the target host, so one disturbance locks once.

kindstring (required)

execforwardnotify

All other fields2 fields

client_iduuid · nullable

The incident's client, null at company level. The key must reach it.

incident_iduuid · nullable

The incident the action belongs to. If the machine stops mid-run, the incident history shows the outcome unknown here.

## Returns

A successful request returns `200` with application/json.

Response fields 1 top-level fields

| Field | Type | Description |
| --- | --- | --- |
| `data` | RunnerLockView | |
| RunnerLockView · 6 fields FieldTypeDescription `acquired` boolean True: run the action. False: another machine holds the lock until expires_at, or already ran this event; then expires_at is now or earlier. `expires_at` string (date-time) `holder` RunnerLockHolderView Only when not acquired: the machine that holds the lock or ran this event. RunnerLockHolderView · 2 fields FieldTypeDescription `name` string `runner_id` uuid `token` uuid Only when acquired. Ends the run with POST /runner/locks/{token}/finish. | | |

## Errors

Only the errors this operation can return. Every error uses the [same envelope](https://upfour.io/docs/api/responses.md).

| Status | Code | Meaning |
| --- | --- | --- |
| 400 | [bad_request](https://upfour.io/docs/api/errors#code-bad_request) | The request is malformed or fails validation. |
| 404 | [not_found](https://upfour.io/docs/api/errors#code-not_found) | The requested resource does not exist or is outside the caller's scope. |
| 503 | [service_unavailable](https://upfour.io/docs/api/errors#code-service_unavailable) | The service or a required dependency is temporarily unavailable. |
| 400 | documented response | A missing key, event_id or handler, an unknown kind, or a hold_ms that is not positive or exceeds one day. |
| 404 | documented response | The client is outside this key's scope, or the machine in X-Up4-Runner-ID is not connected with this key. |

Every call can also return `unauthorized` (401), `rate_limit_exceeded` (429) or `internal_error` (500 / 502). See [Responses and errors](https://upfour.io/docs/api/responses.md).

[PreviousGet a recent event](https://upfour.io/docs/api/runner/get-a-recent-event.md) [NextEnd an action lock](https://upfour.io/docs/api/runner/end-an-action-lock.md)

## Request

### cURL

```bash
curl -s -X POST "https://app.upfour.io/v1/runner/locks" \
  -H "Authorization: Bearer $UP4_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "client_id": "00000000-0000-4000-8000-000000000001",
    "event_id": "00000000-0000-4000-8000-000000000002",
    "handler": "restart-nginx",
    "hold_ms": 60000,
    "incident_id": "00000000-0000-4000-8000-000000000003",
    "key": "721378bc96ef2ddece4a66b447ba60d6a1266860d9cb23f4e0e6c152f30279cb",
    "kind": "exec"
  }'
```

### JavaScript

```javascript
const res = await fetch("https://app.upfour.io/v1/runner/locks", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.UP4_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    client_id: "00000000-0000-4000-8000-000000000001",
    event_id: "00000000-0000-4000-8000-000000000002",
    handler: "restart-nginx",
    hold_ms: 60000,
    incident_id: "00000000-0000-4000-8000-000000000003",
    key: "721378bc96ef2ddece4a66b447ba60d6a1266860d9cb23f4e0e6c152f30279cb",
    kind: "exec",
  }),
});
console.log(res.status, await res.json());
```

### Python

```python
import os
import requests

res = requests.post(
    "https://app.upfour.io/v1/runner/locks",
    headers={"Authorization": f"Bearer {os.environ['UP4_API_KEY']}"},
    json={
        "client_id": "00000000-0000-4000-8000-000000000001",
        "event_id": "00000000-0000-4000-8000-000000000002",
        "handler": "restart-nginx",
        "hold_ms": 60000,
        "incident_id": "00000000-0000-4000-8000-000000000003",
        "key": "721378bc96ef2ddece4a66b447ba60d6a1266860d9cb23f4e0e6c152f30279cb",
        "kind": "exec",
    },
)
print(res.status_code, res.json())
```

### Go

```go
package main

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

func main() {
	payload := strings.NewReader(`{
  "client_id": "00000000-0000-4000-8000-000000000001",
  "event_id": "00000000-0000-4000-8000-000000000002",
  "handler": "restart-nginx",
  "hold_ms": 60000,
  "incident_id": "00000000-0000-4000-8000-000000000003",
  "key": "721378bc96ef2ddece4a66b447ba60d6a1266860d9cb23f4e0e6c152f30279cb",
  "kind": "exec"
}`)
	req, _ := http.NewRequest("POST", "https://app.upfour.io/v1/runner/locks", payload)
	req.Header.Set("Authorization", "Bearer "+os.Getenv("UP4_API_KEY"))
	req.Header.Set("Content-Type", "application/json")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	out, _ := io.ReadAll(res.Body)
	fmt.Println(res.StatusCode, string(out))
}
```

### PHP

```php
<?php
$payload = <<<'JSON'
{
  "client_id": "00000000-0000-4000-8000-000000000001",
  "event_id": "00000000-0000-4000-8000-000000000002",
  "handler": "restart-nginx",
  "hold_ms": 60000,
  "incident_id": "00000000-0000-4000-8000-000000000003",
  "key": "721378bc96ef2ddece4a66b447ba60d6a1266860d9cb23f4e0e6c152f30279cb",
  "kind": "exec"
}
JSON;

$ch = curl_init("https://app.upfour.io/v1/runner/locks");
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("UP4_API_KEY"),
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => $payload,
]);
$response = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE), " ", $response, "\n";
curl_close($ch);
```

## Response

### 200

```json
{
  "data": {
    "acquired": true,
    "expires_at": "2026-10-27T09:00:00Z",
    "token": "00000000-0000-4000-8000-000000000004"
  }
}
```

### 400

```json
{
  "error": {
    "code": "bad_request",
    "message": "The request is malformed or fails validation.",
    "request_id": "req_…"
  }
}
```

### 404

```json
{
  "error": {
    "code": "not_found",
    "message": "The requested resource does not exist or is outside the caller's scope.",
    "request_id": "req_…"
  }
}
```

### 503

```json
{
  "error": {
    "code": "service_unavailable",
    "message": "The service or a required dependency is temporarily unavailable.",
    "request_id": "req_…"
  }
}
```
