Use withToken() on the Laravel HTTP client to attach Authorization Bearer tokens without manual header string formatting.
When sending authenticated requests to third-party APIs, manually setting ['headers' => ['Authorization' => 'Bearer ' . $token]] is repetitive.
The Laravel HTTP client provides the withToken() method.
Basic Usage
use Illuminate\Support\Facades\Http;
// Attaches 'Authorization: Bearer <token>' header automatically
$response = Http::withToken($apiToken)
->get('https://api.github.com/user/repos');
Custom Token Types
If an API uses a custom authentication scheme (such as Basic or Token instead of Bearer), pass the prefix as the second argument:
// Attaches 'Authorization: Token <key>'
$response = Http::withToken($accessKey, 'Token')
->post('https://api.custom-service.com/events', $payload);
Summary
- Shorthand for attaching
Authorizationheaders. - Defaults to the standard
Bearertoken prefix. - Cleanly replaces manual header array concatenation.
Related Tips
View all tips →Handle Hybrid API and Web Controllers with $request->expectsJson()
Use $request->expectsJson() in hybrid controllers to serve formatted JSON responses to API/AJAX requests and redirects to standard web browsers.
Throw Exceptions on HTTP Client Errors with throw() and throwIf()
Use throw(), throwIf(), and throwUnless() on Laravel HTTP client responses to handle 4xx and 5xx API errors with standard exception workflows.