We have all been there, we want to integrate with a 3rd party API in Laravel and we ask ourselves “How should I do this?“. When it comes to API integrations I am no stranger, but still each time I wonder what is going to be the best way.

Refactor your request class to look like the below example: class ListRepositoryWorkflowsRequest extends SaloonRequest { protected ?string $connector = GitHubConnector::class; protected ?string $method = Saloon::GET; public function __construct( public string $owner, public string $repo, ) {} public function defineEndpoint(): string { return "/repos/{$this->owner}/{$this->repo}/actions/workflows"; }}

Now we need to add our authentication token and actually send the request: public function handle(): int { $owner = (string) $this->argument('owner'); $repo = (string) $this->argument('repo'); $request = new ListRepositoryWorkflowsRequest( owner: $owner, repo: $repo, ); $request->withTokenAuth( token: (string) config('services.github.token'), ); $this->info( string: "Fetching workflows for {$owner}/{$repo}", ); $response = $request->send(); return self::SUCCESS; }

'; protected $description = 'Create a new workflow dispatch event for a repository.'; public function handle(): int { $owner = (string) $this->argument('owner'); $repo = (string) $this->argument('repo'); $workflow = (string) $this->argument('workflow'); $request = new CreateWorkflowDispatchEventRequest( owner: $owner, repo: $repo, workflow: $workflow, ); $request->withTokenAuth( token: (string) config('services.github.token'), ); if ($this->hasArgument('branch')) { $request->setData( data: ['ref' => $this->argument('branch')], ); } $this->info( string: "Requesting a new workflow dispatch for {$owner}/{$repo} using workflow: {$workflow}", ); $response = $request->send(); if ($response->failed()) { throw $response->toException(); } $this->info( string: 'Request was accepted by GitHub', ); return self::SUCCESS; }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners