HEXUXIU/M365-Copilot2API

Microsoft 365 Copilot → OpenAI / Anthropic 兼容 API 网关。

What it is

M365‑Copilot2API is a self‑hosted gateway written in Go that lets you treat Microsoft 365 Copilot (the ChatHub service behind the commercial subscription) as a standard OpenAI‑compatible or Anthropic‑compatible API. It translates the private WebSocket protocol used by Copilot into the familiar /v1/chat/completions, /v1/messages, etc. endpoints, so any client that talks to OpenAI or Claude‑style APIs (Claude Code, OpenCode, Cursor, Codex, …) can call Copilot without changes.

Core ideas

Idea How it works
Protocol bridge An internal chathub module handles the Copilot WebSocket handshake, heart‑beats, token streaming, tool calls and multimodal (image) uploads. The bridge presents a clean HTTP/JSON API on the outside.
OpenAI & Anthropic compatibility Exposes /v1/chat/completions, /v1/responses (OpenAI), /v1/messages (Anthropic) and /v1/images/generations. Supports streaming (stream:true), function‑calling conversion, and image input via the OpenAI image_url format.
Session reuse Requests can carry X‑M365‑Session‑Id to bind to a specific cloud‑side conversation, or the gateway can automatically reuse a conversation based on a content key (exact prefix match or similarity‑based match). When a match is found only the new messages are sent upstream, saving tokens.
Multi‑account & fail‑over You can add several Microsoft 365 accounts. The gateway rotates traffic, monitors health, and automatically switches to another account if one fails.
Proxy pool Optional HTTP/HTTPS/SOCKS5 proxy rotation with health checks, useful when the upstream service is accessed through proxies.
Web admin console A single‑page UI for admin login, account OAuth/PKCE linking, API‑key creation, proxy management, usage statistics, conversation browsing and settings.

Who would use it

  • Developers who have a Microsoft 365 Copilot subscription and want to reuse existing OpenAI‑compatible client libraries or tools without rewriting code.
  • Researchers experimenting with Copilot’s LLM capabilities but needing a standard API surface for benchmarking or integration with other frameworks.
  • Personal self‑hosters who want a local “API‑relay” that adds session caching, usage tracking, and a UI while keeping the Copilot backend.

Quick start (summary from the README)

  1. Download a pre‑built binary for your OS from the GitHub releases page.
  2. Set an admin password (environment variable M365_ADMIN_PASSWORD).
  3. Run the binary – it listens on 127.0.0.1:4141 by default.
  4. Open a browser to that address, log in with the admin password, and use the UI to:
    • Authorize one or more Microsoft 365 accounts via the PKCE OAuth flow.
    • Create an API key.
  5. Call the gateway like any OpenAI service, e.g.:
    curl http://127.0.0.1:4141/v1/chat/completions \
         -H "Authorization: Bearer <YOUR_API_KEY>" \
         -H "Content-Type: application/json" \
         -d '{"model":"gpt-5.6-sol","messages":[{"role":"user","content":"你好"}]}'
    

Notable features

  • Streaming SSE – token‑by‑token output works with stream:true.
  • Tool/function calling – OpenAI‑style function calls are mapped to Copilot’s tool protocol (router mode works out‑of‑the‑box).
  • Multimodal – you can send images via base64 data URLs or HTTPS URLs; the gateway uploads them to Copilot and injects the file reference.
  • Image generation – /v1/images/generations forwards to Copilot’s image model (flux‑3/4).
  • Usage logging – per‑key, per‑model, per‑endpoint stats are written to usage.jsonl and shown in the dashboard.
  • Automatic cleanup – idle cloud conversations are reclaimed after a configurable TTL (default 2 h) or when a max count is exceeded.

Limitations / warnings (as stated in the README)

  • This is not an official Microsoft product and has no affiliation with Microsoft, OpenAI, or Anthropic.
  • Using third‑party accounts or proxy tricks may violate the service’s Terms of Service; the user assumes all risk.
  • The project is released under AGPL‑3.0 with a non‑commercial relay restriction – you may not sell a hosted API service built from it.
  • The gateway only runs on the local network by default; exposing it publicly requires your own TLS reverse proxy and proper security hardening.

Where to find more

  • Full API reference (/v1/models, /v1/chat/completions, /v1/messages, /v1/images/generations, /v1/sessions, admin /api/*).
  • Source layout (cmd/server, internal/web, internal/chathub, static web/ UI, helper scripts).
  • Contribution guide and test suite (go test ./...).

All details above are taken directly from the repository’s README; no additional features have been inferred.

Related

  • Project
  • Project
  • Project
  • Dispatch
  • Project