Repository navigation
Expand file tree
/
Copy pathclaude-code-api.service
More file actions
79 lines (70 loc) · 3.58 KB
/
Copy pathclaude-code-api.service
File metadata and controls
79 lines (70 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# claude-code-api: systemd USER unit.
#
# Install with:
# # 1. Mint the shared secret FIRST. Without it the service starts but answers
# # 401 on every route except /health. That is deliberate (see SECURITY.md);
# # it is not a bug to work around.
# umask 077
# printf 'CCAPI_TOKEN=%s\n' "$(python3 -c 'import secrets;print(secrets.token_urlsafe(32))')" \
# > ~/.config/claude-code-api.env
# chmod 600 ~/.config/claude-code-api.env
#
# # 2. Then install the unit.
# cp claude-code-api.service ~/.config/systemd/user/
# systemctl --user daemon-reload
# systemctl --user enable --now claude-code-api.service
#
# This is a USER unit, not a system unit. That is deliberate: the server spawns
# `claude --print`, which needs the invoking user's HOME, PATH, and
# ~/.claude/.credentials.json. Running it system-wide as root or as a dedicated
# service account would not have those, and would widen an endpoint that already
# executes arbitrary commands as whoever runs it. See SECURITY.md.
#
# %h expands to the user's home directory. Adjust the checkout path below if you
# cloned somewhere other than ~/clawd/skcapstone-repos/claude-code-api.
[Unit]
Description=Claude Code API: OpenAI-compatible wrapper for `claude --print` (loopback :18782, token-gated)
Documentation=https://github.com/chefboyrdave21/claude-code-api/blob/main/SOP.md
After=network.target
[Service]
Type=simple
ExecStart=%h/.skenv/bin/python3 %h/clawd/skcapstone-repos/claude-code-api/server.py --port 18782
Restart=always
RestartSec=5
KillMode=control-group
# prb-bd79dd5f: RestartSec x (Burst - 1) must be >= StartLimitIntervalSec, or the
# start limiter never engages and a 203/EXEC unit retries forever. This unit sets
# no StartLimitBurst, so it relies on escalating backoff instead of a hard stop.
# That is intentional (SERVICE_UNIT_STANDARD Tier A: it must never permanently
# die), but it means a broken ExecStart will loop. Watch the journal after any
# change to the path above.
RestartSteps=8
RestartMaxDelaySec=5min
Environment=HOME=%h
Environment=TMPDIR=/tmp
# Never inherit a login-session OAuth snapshot. The CLI and SDK read the
# rotating credential from ~/.claude/.credentials.json instead.
UnsetEnvironment=CLAUDE_CODE_OAUTH_TOKEN
# The shared secret every route except GET /health requires. It lives in a
# 0600 file OUTSIDE the git checkout, never in this tracked unit and never in an
# Environment= line: `systemctl show` and `systemctl cat` both print Environment=
# values in the clear to anyone who can read the unit.
#
# The leading `-` means "do not fail to start if the file is absent". That is on
# purpose. A missing file must not turn into a boot loop under Restart=always
# with no StartLimitBurst; the server handles the unconfigured case itself by
# refusing every authenticated request with a 401 that names this variable, while
# keeping /health answering so the state is visible. See the CCAPI_TOKEN block at
# the top of server.py.
EnvironmentFile=-%h/.config/claude-code-api.env
# Cap concurrent `claude --print` subprocesses. 3 lets background jobs run
# without queueing behind a single interactive turn. See server.py `sem()`.
Environment=CCAPI_MAX_CONCURRENT=3
# SECURITY: setting CLAUDE_API_LOAD_MCP=1 boots the host's configured MCP servers
# inside every spawned subprocess, widening the blast radius from local tools to
# everything those servers expose. Leave it unset. See SECURITY.md.
# `claude` must be on PATH. Set it explicitly: a user unit does not inherit your
# login shell's PATH.
Environment=PATH=%h/.skenv/bin:%h/.local/bin:%h/.npm-global/bin:/usr/local/bin:/usr/bin:/bin
[Install]
WantedBy=default.target