Search code examples
githubcurlgithub-apigithub-cli

How to create a webhook from curl or github cli


How do you create a webhook from curl or github cli?

This doc. does not help a lot: https://docs.github.com/en/rest/reference/repos#create-a-repository-webhook--code-samples

Tried this:

curl -u <user>:<token>\
    -X POST \
    -H "Accept: application/vnd.github.v3+json" \
    https://api.github.com/repos/<org>/<repo>/hooks \
    -d '{"name":"name"}'

Leaves me with questions:

  • What is -d '{"name":"name"}'
  • How do you add a config

Errors:

{
    "message": "Validation Failed",
    "errors": [
    {
        "resource": "Hook",
        "code": "custom",
        "message": "Config must contain URL for webhooks"
    }
    ],
    "documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-webhook"
}

Solution

  • Using curl

    You can use the following to create a webhook:

    curl "https://api.github.com/repos/<org>/<repo>/hooks" \
         -H "Authorization: Token YOUR_TOKEN" \
         -d @- << EOF
    {
      "name": "web",
      "active": true,
      "events": [
        "push"
      ],
      "config": {
        "url": "http://some_webhook.ngrok.io/webhook",
        "content_type": "json"
      }
    }
    EOF
    

    From this doc, name property should have the value web

    Name Type In Description
    name string body Use web to create a webhook. Default: web. This parameter only accepts the value web.

    Possible webhook events are listed here

    Using Github CLI

    gh api /repos/<org>/<repo>/hooks \
       --input - <<< '{
      "name": "web",
      "active": true,
      "events": [
        "watch"
      ],
      "config": {
        "url": "https://some_webhook.ngrok.io/webhook",
        "content_type": "json"
      }
    }'
    

    Checkout gh api