github-copilot

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GitHub Copilot API

GitHub Copilot API

Use the GitHub Copilot REST API via direct
curl
calls to manage Copilot subscriptions and retrieve usage metrics for your organization.
Official docs:
https://docs.github.com/en/rest/copilot
Note: This API is for managing Copilot subscriptions and viewing metrics, not for code generation.

你可以直接通过
curl
调用GitHub Copilot REST API,为你的组织管理Copilot订阅、获取使用指标
官方文档:
https://docs.github.com/en/rest/copilot
注意: 该API用于管理Copilot订阅和查看指标,不具备代码生成能力。

When to Use

使用场景

Use this skill when you need to:
  • Manage Copilot seat assignments (add/remove users and teams)
  • View Copilot billing information for an organization
  • Retrieve usage metrics (active users, code completions, chat usage)
  • Monitor Copilot adoption across teams
  • Audit Copilot usage for compliance

当你需要完成以下操作时可使用该技能:
  • 管理Copilot席位分配(添加/移除用户和团队)
  • 查看组织的Copilot账单信息
  • 获取使用指标(活跃用户、代码补全、聊天使用量)
  • 监控Copilot在各团队的采纳情况
  • 审核Copilot使用情况以满足合规要求

Prerequisites

前置要求

  1. You need a GitHub organization with Copilot Business or Enterprise
  2. Generate a Personal Access Token (PAT) or use a GitHub App
  3. Required permissions:
    manage_billing:copilot
    or
    admin:org
  4. Store your token in the environment variable
    GITHUB_TOKEN
bash
export GITHUB_TOKEN="ghp_xxxxxxxxxxxx"
  1. 你需要有开通了Copilot Business或Enterprise的GitHub组织
  2. 生成个人访问令牌(PAT)或使用GitHub App
  3. 所需权限:
    manage_billing:copilot
    admin:org
  4. 将你的令牌存储在环境变量
    GITHUB_TOKEN
bash
export GITHUB_TOKEN="ghp_xxxxxxxxxxxx"

Token Permissions

令牌权限

  • manage_billing:copilot
    - For billing and seat management
  • read:org
    - For reading organization data
  • admin:org
    - For adding/removing users and teams

Important: When using
$VAR
in a command that pipes to another command, wrap the command containing
$VAR
in
bash -c '...'
. Due to a Claude Code bug, environment variables are silently cleared when pipes are used directly.
bash
bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"'
  • manage_billing:copilot
    - 用于账单和席位管理
  • read:org
    - 用于读取组织数据
  • admin:org
    - 用于添加/移除用户和团队

重要提示: 如果你在需要管道传输到其他命令的指令中使用
$VAR
,请将包含
$VAR
的命令包裹在
bash -c '...'
中。由于Claude Code存在漏洞,直接使用管道时环境变量会被静默清除。
bash
bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"'

How to Use

使用方法

All examples below assume you have
GITHUB_TOKEN
set.
Base URL:
https://api.github.com
Required headers:
  • Authorization: Bearer ${GITHUB_TOKEN}
  • Accept: application/vnd.github+json
  • X-GitHub-Api-Version: 2022-11-28

以下所有示例都假设你已经设置了
GITHUB_TOKEN
基础URL:
https://api.github.com
必填请求头:
  • Authorization: Bearer ${GITHUB_TOKEN}
  • Accept: application/vnd.github+json
  • X-GitHub-Api-Version: 2022-11-28

1. Get Copilot Billing Information

1. 获取Copilot账单信息

Get seat breakdown and settings for an organization. Replace
your-org-name
with your organization name:
bash
bash -c 'curl -s -X GET "https://api.github.com/orgs/your-org-name/copilot/billing" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28"'
Response:
json
{
  "seat_breakdown": {
  "total": 12,
  "added_this_cycle": 2,
  "pending_cancellation": 0,
  "pending_invitation": 1,
  "active_this_cycle": 12,
  "inactive_this_cycle": 0
  },
  "seat_management_setting": "assign_selected",
  "public_code_suggestions": "block"
}

获取组织的席位明细和设置。将
your-org-name
替换为你的组织名称:
bash
bash -c 'curl -s -X GET "https://api.github.com/orgs/your-org-name/copilot/billing" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28"'
响应示例:
json
{
  "seat_breakdown": {
  "total": 12,
  "added_this_cycle": 2,
  "pending_cancellation": 0,
  "pending_invitation": 1,
  "active_this_cycle": 12,
  "inactive_this_cycle": 0
  },
  "seat_management_setting": "assign_selected",
  "public_code_suggestions": "block"
}

2. List All Copilot Seat Assignments

2. 列出所有Copilot席位分配情况

Get all users with Copilot seats. Replace
your-org-name
with your organization name:
bash
bash -c 'curl -s -X GET "https://api.github.com/orgs/your-org-name/copilot/billing/seats?per_page=50" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28"' | jq '.seats[] | {login: .assignee.login, last_activity: .last_activity_at}'

获取所有拥有Copilot席位的用户。将
your-org-name
替换为你的组织名称:
bash
bash -c 'curl -s -X GET "https://api.github.com/orgs/your-org-name/copilot/billing/seats?per_page=50" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28"' | jq '.seats[] | {login: .assignee.login, last_activity: .last_activity_at}'

3. Get Copilot Seat Details for a User

3. 获取单个用户的Copilot席位详情

Get specific user's Copilot seat information. Replace
your-org-name
with your organization name and
username
with the target username:
bash
bash -c 'curl -s -X GET "https://api.github.com/orgs/your-org-name/members/username/copilot" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28"'

获取指定用户的Copilot席位信息。将
your-org-name
替换为你的组织名称,
username
替换为目标用户名:
bash
bash -c 'curl -s -X GET "https://api.github.com/orgs/your-org-name/members/username/copilot" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28"'

4. Add Users to Copilot Subscription

4. 为用户添加Copilot订阅

Assign Copilot seats to specific users. Replace
your-org-name
with your organization name:
Write to
/tmp/github_copilot_request.json
:
json
{
  "selected_usernames": ["user1", "user2"]
}
Then run:
bash
bash -c 'curl -s -X POST "https://api.github.com/orgs/your-org-name/copilot/billing/selected_users" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28" --header "Content-Type: application/json" -d @/tmp/github_copilot_request.json'
Response:
json
{
  "seats_created": 2
}

为指定用户分配Copilot席位。将
your-org-name
替换为你的组织名称:
写入内容到
/tmp/github_copilot_request.json
json
{
  "selected_usernames": ["user1", "user2"]
}
然后运行:
bash
bash -c 'curl -s -X POST "https://api.github.com/orgs/your-org-name/copilot/billing/selected_users" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28" --header "Content-Type: application/json" -d @/tmp/github_copilot_request.json'
响应示例:
json
{
  "seats_created": 2
}

5. Remove Users from Copilot Subscription

5. 移除用户的Copilot订阅

Remove Copilot seats from specific users. Replace
your-org-name
with your organization name:
Write to
/tmp/github_copilot_request.json
:
json
{
  "selected_usernames": ["user1", "user2"]
}
Then run:
bash
bash -c 'curl -s -X DELETE "https://api.github.com/orgs/your-org-name/copilot/billing/selected_users" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28" --header "Content-Type: application/json" -d @/tmp/github_copilot_request.json'
Response:
json
{
  "seats_cancelled": 2
}

收回指定用户的Copilot席位。将
your-org-name
替换为你的组织名称:
写入内容到
/tmp/github_copilot_request.json
json
{
  "selected_usernames": ["user1", "user2"]
}
然后运行:
bash
bash -c 'curl -s -X DELETE "https://api.github.com/orgs/your-org-name/copilot/billing/selected_users" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28" --header "Content-Type: application/json" -d @/tmp/github_copilot_request.json'
响应示例:
json
{
  "seats_cancelled": 2
}

6. Add Teams to Copilot Subscription

6. 为团队添加Copilot订阅

Assign Copilot to entire teams. Replace
your-org-name
with your organization name:
Write to
/tmp/github_copilot_request.json
:
json
{
  "selected_teams": ["engineering", "design"]
}
Then run:
bash
bash -c 'curl -s -X POST "https://api.github.com/orgs/your-org-name/copilot/billing/selected_teams" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28" --header "Content-Type: application/json" -d @/tmp/github_copilot_request.json'

为整个团队分配Copilot权限。将
your-org-name
替换为你的组织名称:
写入内容到
/tmp/github_copilot_request.json
json
{
  "selected_teams": ["engineering", "design"]
}
然后运行:
bash
bash -c 'curl -s -X POST "https://api.github.com/orgs/your-org-name/copilot/billing/selected_teams" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28" --header "Content-Type: application/json" -d @/tmp/github_copilot_request.json'

7. Remove Teams from Copilot Subscription

7. 移除团队的Copilot订阅

Remove Copilot from teams. Replace
your-org-name
with your organization name:
Write to
/tmp/github_copilot_request.json
:
json
{
  "selected_teams": ["engineering"]
}
Then run:
bash
bash -c 'curl -s -X DELETE "https://api.github.com/orgs/your-org-name/copilot/billing/selected_teams" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28" --header "Content-Type: application/json" -d @/tmp/github_copilot_request.json'

收回整个团队的Copilot权限。将
your-org-name
替换为你的组织名称:
写入内容到
/tmp/github_copilot_request.json
json
{
  "selected_teams": ["engineering"]
}
然后运行:
bash
bash -c 'curl -s -X DELETE "https://api.github.com/orgs/your-org-name/copilot/billing/selected_teams" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28" --header "Content-Type: application/json" -d @/tmp/github_copilot_request.json'

8. Get Copilot Usage Metrics for Organization

8. 获取组织的Copilot使用指标

Get usage statistics (requires 5+ active users). Replace
your-org-name
with your organization name:
bash
bash -c 'curl -s -X GET "https://api.github.com/orgs/your-org-name/copilot/metrics?per_page=7" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28"' | jq '.[] | {date, total_active_users, total_engaged_users}'
Response:
json
{
  "date": "2024-06-24",
  "total_active_users": 24,
  "total_engaged_users": 20
}

获取使用统计数据(需要≥5名活跃用户)。将
your-org-name
替换为你的组织名称:
bash
bash -c 'curl -s -X GET "https://api.github.com/orgs/your-org-name/copilot/metrics?per_page=7" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28"' | jq '.[] | {date, total_active_users, total_engaged_users}'
响应示例:
json
{
  "date": "2024-06-24",
  "total_active_users": 24,
  "total_engaged_users": 20
}

9. Get Copilot Metrics for a Team

9. 获取单个团队的Copilot指标

Get team-specific usage metrics. Replace
your-org-name
with your organization name and
team-name
with the target team:
bash
bash -c 'curl -s -X GET "https://api.github.com/orgs/your-org-name/team/team-name/copilot/metrics" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28"'

获取指定团队的使用指标。将
your-org-name
替换为你的组织名称,
team-name
替换为目标团队名称:
bash
bash -c 'curl -s -X GET "https://api.github.com/orgs/your-org-name/team/team-name/copilot/metrics" --header "Authorization: Bearer ${GITHUB_TOKEN}" --header "Accept: application/vnd.github+json" --header "X-GitHub-Api-Version: 2022-11-28"'

Metrics Data Structure

指标数据结构

The metrics response includes:
FieldDescription
total_active_users
Users with Copilot activity
total_engaged_users
Users who accepted suggestions
copilot_ide_code_completions
Code completion stats by language/editor
copilot_ide_chat
IDE chat usage stats
copilot_dotcom_chat
GitHub.com chat usage
copilot_dotcom_pull_requests
PR summary usage

指标响应包含以下字段:
字段描述
total_active_users
有Copilot活动记录的用户
total_engaged_users
接受了补全建议的用户
copilot_ide_code_completions
按语言/编辑器统计的代码补全数据
copilot_ide_chat
IDE聊天功能的使用统计
copilot_dotcom_chat
GitHub.com聊天功能的使用情况
copilot_dotcom_pull_requests
PR摘要功能的使用情况

Guidelines

使用规范

  1. Requires Copilot Business/Enterprise: Free tier users cannot use this API
  2. Metrics need 5+ users: Usage metrics only available with 5+ active Copilot users
  3. Data retention: Metrics available for up to 100 days
  4. Rate limits: Standard GitHub API rate limits apply
  5. API in preview: Some endpoints may change
  1. 需要Copilot Business/Enterprise版本:免费版用户无法使用该API
  2. 指标要求5名以上用户:只有当活跃Copilot用户数≥5时才可查看使用指标
  3. 数据保留期:指标最多保留100天
  4. 速率限制:遵循GitHub API标准速率限制
  5. API处于预览阶段:部分端点可能会变动