{"meta":{"title":"Backend services setup","intro":"Run the Copilot SDK in server-side applications—APIs, web backends, microservices, and background workers. The CLI runs as a headless server that your backend code connects to over the network.","product":"GitHub Copilot","breadcrumbs":[{"href":"/en/enterprise-cloud@latest/copilot","title":"GitHub Copilot"},{"href":"/en/enterprise-cloud@latest/copilot/how-tos","title":"How-tos"},{"href":"/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk","title":"Copilot SDK"},{"href":"/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup","title":"Set up Copilot SDK"},{"href":"/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup/backend-services","title":"Backend Services"}],"documentType":"article"},"body":"# Backend services setup\n\nRun the Copilot SDK in server-side applications—APIs, web backends, microservices, and background workers. The CLI runs as a headless server that your backend code connects to over the network.\n\n<!-- markdownlint-disable GHD046 GHD005 -->\n\n<!-- Suppressed: GHD046 (outdated release terminology), GHD005 (hardcoded data variable) -->\n\n**Best for:** Web app backends, API services, internal tools, CI/CD integrations, any server-side workload.\n\n## How it works\n\nInstead of the SDK spawning a CLI child process, you run the CLI independently in **headless server mode**. Your backend connects to it over TCP using the `Connection` option (`URIConnection`).\n\n![Diagram: Flowchart showing the described process.](/assets/images/help/copilot/copilot-sdk/setup-backend-services-diagram-0.png)\n\n**Key characteristics:**\n\n* CLI runs as a persistent server process (not spawned per request)\n* SDK connects over TCP—CLI and app can run in different containers\n* Multiple SDK clients can share one CLI server\n* Works with any auth method (GitHub tokens, env vars, BYOK)\n\nFor multi-user server mode, configure SDK clients with `mode: \"empty\"`, pass user credentials per session, and explicitly allow tools for each session. See [Multi-tenancy and server deployments](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup/multi-tenancy) for the full pattern.\n\n## Architecture: auto-managed vs. external CLI\n\n![Diagram: Flowchart showing the described process.](/assets/images/help/copilot/copilot-sdk/setup-backend-services-diagram-1.png)\n\n## Step 1: start the CLI in headless mode\n\nRun the CLI as a background server:\n\n```bash\n# Start with a specific port\ncopilot --headless --port 4321\n\n# Or let it pick a random port (prints the URL)\ncopilot --headless\n# Output: Listening on http://localhost:52431\n```\n\nBy default the headless server only accepts connections from loopback (`127.0.0.1`). To accept connections from other hosts—for example from another machine on your network—bind to a non-loopback address with `--host`:\n\n```bash\ncopilot --headless --host 0.0.0.0 --port 4321\n```\n\nFor production, run it as a system service or in a container.\n\n> \\[!NOTE]\n> There is no official pre-built Docker image for the Copilot CLI. You can build your own from the [GitHub releases](https://github.com/github/copilot-cli/releases):\n\n```dockerfile\nFROM debian:bookworm-slim\nARG COPILOT_VERSION=1.0.7\nRUN apt-get update \\\n    && apt-get install -y --no-install-recommends ca-certificates wget \\\n    && ARCH=$(dpkg --print-architecture) \\\n    && case \"${ARCH}\" in amd64) COPILOT_ARCH=\"x64\" ;; arm64) COPILOT_ARCH=\"arm64\" ;; *) echo \"Unsupported: ${ARCH}\" && exit 1 ;; esac \\\n    && wget -q \"https://github.com/github/copilot-cli/releases/download/v${COPILOT_VERSION}/copilot-linux-${COPILOT_ARCH}.tar.gz\" \\\n    && tar -xzf \"copilot-linux-${COPILOT_ARCH}.tar.gz\" \\\n    && mv copilot /usr/local/bin/ \\\n    && rm \"copilot-linux-${COPILOT_ARCH}.tar.gz\" \\\n    && apt-get purge -y wget && apt-get autoremove -y && rm -rf /var/lib/apt/lists/*\nENTRYPOINT [\"copilot\"]\n```\n\n```bash\n# Build the image\ndocker build --build-arg COPILOT_VERSION=1.0.7 -t copilot-cli:latest .\n\n# For remote deployments (Kubernetes, ACI, etc.), push to your registry\ndocker tag copilot-cli:latest your-registry/copilot-cli:latest\ndocker push your-registry/copilot-cli:latest\n```\n\n```bash\n# Docker — must bind to 0.0.0.0 so the container's published port is reachable\ndocker run -d --name copilot-cli \\\n    -p 4321:4321 \\\n    -e COPILOT_GITHUB_TOKEN=\"$TOKEN\" \\\n    copilot-cli:latest \\\n    --headless --host 0.0.0.0 --port 4321\n\n# systemd\n[Service]\nExecStart=/usr/local/bin/copilot --headless --port 4321\nEnvironment=COPILOT_GITHUB_TOKEN=your-token\nRestart=always\n```\n\n## Step 2: connect the SDK\n\n<div class=\"ghd-codetabs\">\n<div class=\"ghd-codetab\" data-lang=\"typescript\" data-label=\"TypeScript\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">TypeScript</div>\n\n```typescript\nimport { CopilotClient, RuntimeConnection } from \"@github/copilot-sdk\";\n\nconst client = new CopilotClient({\n    connection: RuntimeConnection.forUri(\"localhost:4321\"),\n    mode: \"empty\",\n});\n\nconst session = await client.createSession({\n    sessionId: `user-${userId}-${Date.now()}`,\n    model: \"gpt-5.4\",\n    availableTools: [\"custom:*\"],\n    gitHubToken: user.githubToken,\n});\n\nconst response = await session.sendAndWait({ prompt: req.body.message });\nres.json({ content: response?.data.content });\n```\n\n</div>\n\n<div class=\"ghd-codetab\" data-lang=\"python\" data-label=\"Python\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">Python</div>\n\n```python\nfrom copilot import CopilotClient, RuntimeConnection\nfrom copilot.session import PermissionHandler\n\nclient = CopilotClient(\n    connection=RuntimeConnection.for_uri(\"localhost:4321\"),\n)\nawait client.start()\n\nsession = await client.create_session(on_permission_request=PermissionHandler.approve_all, model=\"gpt-5.4\", session_id=f\"user-{user_id}-{int(time.time())}\")\n\nresponse = await session.send_and_wait(message)\n```\n\n</div>\n\n<div class=\"ghd-codetab\" data-lang=\"go\" data-label=\"Go\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">Go</div>\n\n```golang\nclient := copilot.NewClient(&copilot.ClientOptions{\n    Connection: copilot.URIConnection{URL: \"localhost:4321\"},\n})\nclient.Start(ctx)\ndefer client.Stop()\n\nsession, _ := client.CreateSession(ctx, &copilot.SessionConfig{\n    SessionID: fmt.Sprintf(\"user-%s-%d\", userID, time.Now().Unix()),\n    Model:     \"gpt-5.4\",\n})\n\nresponse, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: message})\n```\n\n</div>\n\n<div class=\"ghd-codetab\" data-lang=\"dotnet\" data-label=\".NET\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">.NET</div>\n\n```csharp\nvar client = new CopilotClient(new CopilotClientOptions\n{\n    Connection = RuntimeConnection.ForUri(\"localhost:4321\"),\n});\n\nawait using var session = await client.CreateSessionAsync(new SessionConfig\n{\n    SessionId = $\"user-{userId}-{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}\",\n    Model = \"gpt-5.4\",\n});\n\nvar response = await session.SendAndWaitAsync(\n    new MessageOptions { Prompt = message });\n```\n\n</div>\n\n<div class=\"ghd-codetab\" data-lang=\"java\" data-label=\"Java\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">Java</div>\n\n```java\nimport com.github.copilot.CopilotClient;\nimport com.github.copilot.rpc.*;\n\nvar userId = \"user1\";\nvar message = \"Hello!\";\n\nvar client = new CopilotClient(new CopilotClientOptions()\n    .setCliUrl(\"localhost:4321\")\n);\n\ntry {\n    client.start().get();\n\n    var session = client.createSession(new SessionConfig()\n        .setSessionId(String.format(\"user-%s-%d\", userId, System.currentTimeMillis() / 1000))\n        .setModel(\"gpt-5.4\")\n        .setOnPermissionRequest(PermissionHandler.APPROVE_ALL)\n    ).get();\n\n    var response = session.sendAndWait(new MessageOptions()\n        .setPrompt(message)).get();\n} finally {\n    client.stop().get();\n}\n```\n\n</div>\n\n</div>\n\n## Authentication for backend services\n\n### Environment variable tokens\n\nThe simplest approach—set a token on the CLI server:\n\n![Diagram: Flowchart showing the described process.](/assets/images/help/copilot/copilot-sdk/setup-backend-services-diagram-2.png)\n\n```bash\n# All requests use this token\nexport COPILOT_GITHUB_TOKEN=\"gho_service_account_token\"\ncopilot --headless --port 4321\n```\n\n### Per-user tokens (OAuth)\n\nPass individual user tokens when creating sessions. See [GitHub OAuth setup](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup/github-oauth) for the full flow.\n\n```typescript\nconst client = new CopilotClient({\n    connection: RuntimeConnection.forUri(\"localhost:4321\"),\n    mode: \"empty\",\n});\n\n// Your API receives user tokens from your auth layer\napp.post(\"/chat\", authMiddleware, async (req, res) => {\n    const session = await client.createSession({\n        sessionId: `user-${req.user.id}-chat`,\n        model: \"gpt-5.4\",\n        availableTools: [\"custom:*\"],\n        gitHubToken: req.user.githubToken,\n    });\n\n    const response = await session.sendAndWait({\n        prompt: req.body.message,\n    });\n\n    res.json({ content: response?.data.content });\n});\n```\n\n### BYOK (no GitHub auth)\n\nUse your own API keys for the model provider. See [BYOK (bring your own key)](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/auth/byok) for details.\n\n```typescript\nconst client = new CopilotClient({\n    connection: RuntimeConnection.forUri(\"localhost:4321\"),\n});\n\nconst session = await client.createSession({\n    model: \"gpt-5.4\",\n    provider: {\n        type: \"openai\",\n        baseUrl: \"https://api.openai.com/v1\",\n        apiKey: process.env.OPENAI_API_KEY,\n    },\n});\n```\n\n## Common backend patterns\n\n### Web API with Express\n\n![Diagram: Flowchart showing the described process.](/assets/images/help/copilot/copilot-sdk/setup-backend-services-diagram-3.png)\n\n```typescript\nimport express from \"express\";\nimport { CopilotClient, RuntimeConnection } from \"@github/copilot-sdk\";\n\nconst app = express();\napp.use(express.json());\n\n// Single shared CLI connection for multi-user server mode\nconst client = new CopilotClient({\n    connection: RuntimeConnection.forUri(process.env.CLI_URL || \"localhost:4321\"),\n    mode: \"empty\",\n});\n\napp.post(\"/api/chat\", async (req, res) => {\n    const { sessionId, message } = req.body;\n\n    // Create or resume session\n    let session;\n    try {\n        session = await client.resumeSession(sessionId);\n    } catch {\n        session = await client.createSession({\n            sessionId,\n            model: \"gpt-5.4\",\n            availableTools: [\"custom:*\"],\n            gitHubToken: req.user.githubToken,\n        });\n    }\n\n    const response = await session.sendAndWait({ prompt: message });\n    res.json({\n        sessionId,\n        content: response?.data.content,\n    });\n});\n\napp.listen(3000);\n```\n\n### Background worker\n\n```typescript\nimport { CopilotClient, RuntimeConnection } from \"@github/copilot-sdk\";\n\nconst client = new CopilotClient({\n    connection: RuntimeConnection.forUri(process.env.CLI_URL || \"localhost:4321\"),\n});\n\n// Process jobs from a queue\nasync function processJob(job: Job) {\n    const session = await client.createSession({\n        sessionId: `job-${job.id}`,\n        model: \"gpt-5.4\",\n    });\n\n    const response = await session.sendAndWait({\n        prompt: job.prompt,\n    });\n\n    await saveResult(job.id, response?.data.content);\n    await session.disconnect();  // Clean up after job completes\n}\n```\n\n### Docker compose deployment\n\n```yaml\nversion: \"3.8\"\n\nservices:\n  copilot-cli:\n    image: copilot-cli:latest  # See \"Step 1\" above for how to build this image\n    command: [\"--headless\", \"--host\", \"0.0.0.0\", \"--port\", \"4321\"]\n    environment:\n      - COPILOT_GITHUB_TOKEN=${COPILOT_GITHUB_TOKEN}\n    ports:\n      - \"4321:4321\"\n    restart: always\n    volumes:\n      - session-data:/root/.copilot/session-state\n\n  api:\n    build: .\n    environment:\n      - CLI_URL=copilot-cli:4321\n    depends_on:\n      - copilot-cli\n    ports:\n      - \"3000:3000\"\n\nvolumes:\n  session-data:\n```\n\n![Diagram: Flowchart showing the described process.](/assets/images/help/copilot/copilot-sdk/setup-backend-services-diagram-4.png)\n\n## Health checks\n\nMonitor the CLI server's health:\n\n```typescript\n// Periodic health check\nasync function checkCLIHealth(): Promise<boolean> {\n    try {\n        const status = await client.getStatus();\n        return status !== undefined;\n    } catch {\n        return false;\n    }\n}\n```\n\n## Session cleanup\n\nBackend services should actively clean up sessions to avoid resource leaks:\n\n```typescript\n// Clean up expired sessions periodically\nasync function cleanupSessions(maxAgeMs: number) {\n    const sessions = await client.listSessions();\n    const now = Date.now();\n\n    for (const session of sessions) {\n        const age = now - new Date(session.createdAt).getTime();\n        if (age > maxAgeMs) {\n            await client.deleteSession(session.sessionId);\n        }\n    }\n}\n\n// Run every hour\nsetInterval(() => cleanupSessions(24 * 60 * 60 * 1000), 60 * 60 * 1000);\n```\n\n## Limitations\n\n| Limitation                                      | Details                                                                                                                |\n| ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |\n| **Single CLI server = single point of failure** | See [Scaling and multi-tenancy](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup/scaling) for HA patterns |\n| **No built-in auth between SDK and CLI**        | Secure the network path (same host, VPC, etc.)                                                                         |\n| **Session state on local disk**                 | Mount persistent storage for container restarts                                                                        |\n| **30-minute idle timeout**                      | Sessions without activity are auto-cleaned                                                                             |\n\n## When to move on\n\n| Need                                     | Next Guide                                                                                                          |\n| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |\n| Multiple CLI servers / high availability | [Scaling and multi-tenancy](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup/scaling)                  |\n| SDK isolation for concurrent users       | [Multi-tenancy and server deployments](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup/multi-tenancy) |\n| GitHub account auth for users            | [GitHub OAuth setup](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup/github-oauth)                    |\n| Your own model keys                      | [BYOK (bring your own key)](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/auth/byok)                      |\n\n## Next steps\n\n* **[Multi-tenancy and server deployments](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup/multi-tenancy)**: Configure SDK isolation for concurrent users\n* **[Scaling and multi-tenancy](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup/scaling)**: Handle more users, add redundancy\n* **[Session resume and persistence](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/features/session-persistence)**: Resume sessions across restarts\n* **[GitHub OAuth setup](/en/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/setup/github-oauth)**: Add user authentication"}