{"id":19324870,"url":"https://github.com/spatie/laravel-github-webhooks","last_synced_at":"2025-05-16T13:02:15.093Z","repository":{"id":48391224,"uuid":"388826004","full_name":"spatie/laravel-github-webhooks","owner":"spatie","description":"Handle GitHub webhooks in a Laravel application","archived":false,"fork":false,"pushed_at":"2025-02-17T10:17:35.000Z","size":99,"stargazers_count":90,"open_issues_count":1,"forks_count":9,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-12T08:37:02.692Z","etag":null,"topics":["github","laravel","php","webhooks"],"latest_commit_sha":null,"homepage":"https://spatie.be/open-source","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"Spatie"}},"created_at":"2021-07-23T14:20:10.000Z","updated_at":"2025-04-04T04:40:52.000Z","dependencies_parsed_at":"2024-03-07T08:43:05.424Z","dependency_job_id":"43e70ae6-52a5-4aaa-893f-a46393bd5165","html_url":"https://github.com/spatie/laravel-github-webhooks","commit_stats":{"total_commits":67,"total_committers":8,"mean_commits":8.375,"dds":0.3731343283582089,"last_synced_commit":"17a3b7ab2e2bbb33d76b30ae2dfda8f15fb3639d"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-github-webhooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-github-webhooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-github-webhooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-github-webhooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-github-webhooks/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253723237,"owners_count":21953588,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["github","laravel","php","webhooks"],"created_at":"2024-11-10T02:07:17.229Z","updated_at":"2025-05-16T13:02:15.072Z","avatar_url":"https://github.com/spatie.png","language":"PHP","readme":"# Handle GitHub webhooks in a Laravel application\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-github-webhooks.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-github-webhooks)\n![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/spatie/laravel-github-webhooks/run-tests.yml?branch=main\u0026label=tests)\n![Check \u0026 fix styling](https://github.com/spatie/laravel-github-webhooks/workflows/Check%20\u0026%20fix%20styling/badge.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-github-webhooks.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-github-webhooks)\n\nGitHub can notify your application of events using webhooks. This package can help you handle\nthose webhooks. \n\nOut of the box, it will verify the GitHub signature of all incoming requests. All valid calls will be\nlogged to the database. The package allows you to easily define jobs or events that should be dispatched when specific webhooks hit your\napp. \n\nHere's an example of such a job.\n\n```php\nnamespace App\\Jobs\\GitHubWebhooks;\n\nuse Illuminate\\Bus\\Queueable;\nuse Illuminate\\Queue\\SerializesModels;\nuse Illuminate\\Queue\\InteractsWithQueue;\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Spatie\\GitHubWebhooks\\Models\\GitHubWebhookCall;\n\nclass HandleIssueOpenedWebhookJob implements ShouldQueue\n{\n    use InteractsWithQueue, Queueable, SerializesModels;\n\n    public GitHubWebhookCall $gitHubWebhookCall;\n\n    public function __construct(\n        public GitHubWebhookCall $webhookCall\n    ) {}\n\n    public function handle()\n    {\n        // React to the issue opened at GitHub event here\n\n        // You can access the payload of the GitHub webhook call with `$this-\u003ewebhookCall-\u003epayload()`\n    }\n}\n```\n\nBefore using this package we highly recommend\nreading [the entire documentation on webhooks over at GitHub](https://docs.github.com/en/developers/webhooks-and-events/webhooks/about-webhooks).\n\n## Are you a visual learner?\n\nIn [this stream on YouTube](https://www.youtube.com/watch?v=BKJbd-VlV88), I show how to use package, go over the source code, and explain how the package is tested.\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-github-webhooks.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-github-webhooks)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can\nsupport us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.\nYou'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards\non [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/laravel-github-webhooks\n```\n\nYou must publish the config file with:\n\n```bash\nphp artisan vendor:publish --provider=\"Spatie\\GitHubWebhooks\\GitHubWebhooksServiceProvider\" --tag=\"github-webhooks-config\"\n```\n\nThis is the contents of the config file that will be published at `config/github-webhooks.php`:\n\n```php\nuse Spatie\\GitHubWebhooks\\Models\\GitHubWebhookCall;\nuse Spatie\\GitHubWebhooks\\Jobs\\ProcessGitHubWebhookJob;\nuse Spatie\\WebhookClient\\WebhookProfile\\ProcessEverythingWebhookProfile;\n\nreturn [\n    /*\n     * GitHub will sign each webhook using a secret. You can find the used secret at the\n     * webhook configuration settings: https://docs.github.com/en/developers/webhooks-and-events/webhooks/about-webhooks.\n     */\n    'signing_secret' =\u003e env('GITHUB_WEBHOOK_SECRET'),\n\n    /*\n     * You can define the job that should be run when a certain webhook hits your application\n     * here.\n     *\n     * You can find a list of GitHub webhook types here:\n     * https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads.\n     * \n     * You can use \"*\" to let a job handle all sent webhook types\n     */\n    'jobs' =\u003e [\n        // 'ping' =\u003e \\App\\Jobs\\GitHubWebhooks\\HandlePingWebhook::class,\n        // 'issues.opened' =\u003e \\App\\Jobs\\GitHubWebhooks\\HandleIssueOpenedWebhookJob::class,\n        // '*' =\u003e \\App\\Jobs\\GitHubWebhooks\\HandleAllWebhooks::class\n    ],\n\n    /*\n     * This model will be used to store all incoming webhooks.\n     * It should be or extend `Spatie\\GitHubWebhooks\\Models\\GitHubWebhookCall`\n     */\n    'model' =\u003e GitHubWebhookCall::class,\n\n    /*\n     * When running `php artisan model:prune` all stored GitHub webhook calls\n     * that were successfully processed will be deleted.\n     *\n     * More info on pruning: https://laravel.com/docs/8.x/eloquent#pruning-models\n     */\n    'prune_webhook_calls_after_days' =\u003e 10,\n\n    /*\n     * The classname of the job to be used. The class should equal or extend\n     * Spatie\\GitHubWebhooks\\ProcessGitHubWebhookJob.\n     */\n    'job' =\u003e ProcessGitHubWebhookJob::class,\n\n    /**\n     * This class determines if the webhook call should be stored and processed.\n     */\n    'profile' =\u003e ProcessEverythingWebhookProfile::class,\n\n    /*\n     * When disabled, the package will not verify if the signature is valid.\n     * This can be handy in local environments.\n     */\n    'verify_signature' =\u003e env('GITHUB_SIGNATURE_VERIFY', true),\n];\n```\n\nIn the `signing_secret` key of the config file you should add a valid webhook secret. You can find the secret used\nat [the webhook configuration settings on the GitHub dashboard](https://dashboard.github.com/account/webhooks).\n\nNext, you must publish the migration with:\n\n```bash\nphp artisan vendor:publish --provider=\"Spatie\\GitHubWebhooks\\GitHubWebhooksServiceProvider\" --tag=\"github-webhooks-migrations\"\n```\n\nAfter the migration has been published, you can create the `github_webhook_calls` table by running the migrations:\n\n```bash\nphp artisan migrate\n```\n\nFinally, take care of the routing: At the GitHub webhooks settings of a repo you must\nconfigure at what URL GitHub webhooks should be sent. In the routes file of your app you must pass that route\nto the `Route::githubWebhooks` route macro:\n\n```php\nRoute::githubWebhooks('webhook-route-configured-at-the-github-webhooks-settings');\n```\n\nMake sure when configuring the webhook url that the webhooks are send as `application/json` and not as `application/x-www-form-urlencoded`.\n\n\nBehind the scenes this macro will register a `POST` route to a controller provided by this package. We recommend to put it in the `api.php` routes file, so no session is created when a webhook comes in, and no CSRF token is needed.\n\n\nShould you, for any reason, have to register the route in your `web.php` routes file, then you must add that route to the `except` array of the `VerifyCsrfToken` middleware:\n\n```php\nprotected $except = [\n    'webhook-route-configured-at-the-github-webhooks-settings',\n];\n```\n\n## Usage\n\nGitHub will send out webhooks for several event types. You can find\nthe [full list of events types](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads)\nin the GitHub documentation.\n\nGitHub will sign all requests hitting the webhook url of your app. This package will automatically verify if the\nsignature is valid. If it is not, the request was probably not sent by GitHub.\n\nUnless something goes terribly wrong, this package will always respond with a `200` to webhook requests. Sending a `200`\nwill prevent GitHub from resending the same event over and over again. All webhook requests with a valid signature will\nbe logged in the `github_webhook_calls` table. The table has a `payload` column where the entire payload of the incoming\nwebhook is saved.\n\nIf the signature is not valid, the request will not be logged in the `github_webhook_calls` table but\na `Spatie\\GitHubWebhooks\\WebhookFailed` exception will be thrown. If something goes wrong during the webhook request the\nthrown exception will be saved in the `exception` column. In that case the controller will send a `500` instead of `200`.\n\nThere are two ways this package enables you to handle webhook requests: you can opt to queue a job or listen to the\nevents the package will fire.\n\n### Handling webhook requests using jobs\n\nIf you want to do something when a specific event type comes in you can define a job that does the work. Here's an\nexample of such a job:\n\n```php\nnamespace App\\Jobs\\GitHubWebhooks;\n\nuse Illuminate\\Bus\\Queueable;\nuse Illuminate\\Queue\\SerializesModels;\nuse Illuminate\\Queue\\InteractsWithQueue;\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Spatie\\GitHubWebhooks\\Models\\GitHubWebhookCall;\n\nclass HandleIssueOpenedWebhookJob implements ShouldQueue\n{\n    use InteractsWithQueue, Queueable, SerializesModels;\n\n    public GitHubWebhookCall $gitHubWebhookCall;\n\n    public function __construct(\n        public GitHubWebhookCall $webhookCall\n    ) {}\n\n    public function handle()\n    {\n        // do your work here\n\n        // you can access the payload of the webhook call with `$this-\u003ewebhookCall-\u003epayload`\n    }\n}\n```\n\nWe highly recommend that you make this job queueable, because this will minimize the response time of the webhook\nrequests. This allows you to handle more GitHub webhook requests and avoid timeouts.\n\nAfter having created your job you must register it at the `jobs` array in the `github-webhooks.php` config file. The key\nshould be the name of [the GitHub event type](https://docs.github.com/en/developers/webhooks-and-events/events/github-event-types). Optionally, you can let it follow with a dot and the value that is in the action key of the payload of a event.\n\n```php\n// config/github-webhooks.php\n\n'jobs' =\u003e [\n    'issues.opened' =\u003e \\App\\Jobs\\GitHubWebhooks\\HandleIssueOpenedWebhookJob::class, // will be called when issues are opened\n    'issues' =\u003e \\App\\Jobs\\GitHubWebhooks\\HandleIssuesWebhookJob::class, // will be called when issues are opened, created, deleted, ...\n    '*' =\u003e \\App\\Jobs\\GitHubWebhooks\\HandleAllWebhooksJob::class, // will be called when any event/action comes in\n],\n```\n\n### Working with a `GitHubWebhookCall` model\n\nThe `Spatie\\GitHubWebhooks\\Models\\GitHubWebhookCall` model contains some handy methods:\n\n- `headers()`: returns an instance of `Symfony\\Component\\HttpFoundation\\HeaderBag` containing all headers used on the request\n- `eventActionName()`: returns the event name and action name of a webhooks, for example `issues.opened`\n- `payload($key = null)`: returns the payload of the webhook as an array. Optionally, you can pass a key in the payload which value you needed. For deeply nested values you can use dot notation (example: `$githubWebhookCall-\u003epayload('issue.user.login');`).\n\n### Handling webhook requests using events\n\nInstead of queueing jobs to perform some work when a webhook request comes in, you can opt to listen to the events this\npackage will fire. Whenever a valid request hits your app, the package will fire\na `github-webhooks::\u003cname-of-the-event\u003e` event.\n\nThe payload of the events will be the instance of `GitHubWebhookCall` that was created for the incoming request.\n\nLet's take a look at how you can listen for such an event. In the `EventServiceProvider` you can register listeners.\n\n```php\n/**\n * The event listener mappings for the application.\n *\n * @var array\n */\nprotected $listen = [\n    'github-webhooks::issues.opened' =\u003e [\n        App\\Listeners\\IssueOpened::class,\n    ],\n];\n```\n\nHere's an example of such a listener:\n\n```php\n\u003c?php\n\nnamespace App\\Listeners;\n\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Spatie\\GitHubWebhooks\\Models\\GitHubWebhookCall;\n\nclass IssueOpened implements ShouldQueue\n{\n    public function handle(GitHubWebhookCall $webhookCall)\n    {\n        // do your work here\n\n        // you can access the payload of the webhook call with `$webhookCall-\u003epayload`\n    }\n}\n```\n\nWe highly recommend that you make the event listener queueable, as this will minimize the response time of the webhook\nrequests. This allows you to handle more GitHub webhook requests and avoid timeouts.\n\nThe above example is only one way to handle events in Laravel. To learn the other options,\nread [the Laravel documentation on handling events](https://laravel.com/docs/5.5/events).\n\n## Deleting processed webhooks\n\nThe `Spatie\\GitHubWebhooks\\Models\\GitHubWebhookCall` is [`MassPrunable`](https://laravel.com/docs/8.x/eloquent#mass-pruning). To delete all processed webhooks every day you can schedule this command.\n\n```php\n$schedule-\u003ecommand('model:prune', [\n    '--model' =\u003e [\\Spatie\\GitHubWebhooks\\Models\\GitHubWebhookCall::class],\n])-\u003edaily();\n```\n\nAll models that are older than the specified amount of days in the `prune_webhook_calls_after_days` key of the `github-webhooks` config file will be deleted.\n\n## Advanced usage\n\n### Retry handling a webhook\n\nAll incoming webhook requests are written to the database. This is incredibly valuable when something goes wrong while\nhandling a webhook call. You can easily retry processing the webhook call, after you've investigated and fixed the cause\nof failure, like this:\n\n```php\nuse Spatie\\GitHubWebhooks\\Models\\GitHubWebhookCall;\nuse Spatie\\GitHubWebhooks\\Jobs\\ProcessGitHubWebhookJob;\n\ndispatch(new ProcessGitHubWebhookJob(GitHubWebhookCall::find($id)));\n```\n\n### Performing custom logic\n\nYou can add some custom logic that should be executed before and/or after the scheduling of the queued job by using your\nown model. You can do this by specifying your own model in the `model` key of the `github-webhooks` config file. The\nclass should extend `Spatie\\GitHubWebhooks\\ProcessGitHubWebhookJob`.\n\nHere's an example:\n\n```php\nuse Spatie\\GitHubWebhooks\\Jobs\\ProcessGitHubWebhookJob;\n\nclass MyCustomGitHubWebhookJob extends ProcessGitHubWebhookJob\n{\n    public function handle()\n    {\n        // do some custom stuff beforehand\n\n        parent::handle();\n\n        // do some custom stuff afterwards\n    }\n}\n```\n\n### Determine if a request should be processed\n\nYou may use your own logic to determine if a request should be processed or not. You can do this by specifying your own\nprofile in the `profile` key of the `github-webhooks` config file. The class should\nimplement `Spatie\\WebhookClient\\WebhookProfile\\WebhookProfile`.\n\nGitHub might occasionally send a webhook\nrequest [more than once](https://github.com/docs/webhooks/best-practices#duplicate-events). In this example we will make\nsure to only process a request if it wasn't processed before.\n\n```php\nuse Illuminate\\Http\\Request;\nuse Spatie\\WebhookClient\\Models\\WebhookCall;\nuse Spatie\\WebhookClient\\WebhookProfile\\WebhookProfile;\n\nclass GitHubWebhookProfile implements WebhookProfile\n{\n    public function shouldProcess(Request $request): bool\n    {\n        return ! WebhookCall::where('payload-\u003eid', $request-\u003eget('id'))-\u003eexists();\n    }\n}\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information about what has changed recently.\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":["https://github.com/sponsors/Spatie"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-github-webhooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-github-webhooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-github-webhooks/lists"}