Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Conixs's avatar

Third party API headers

Hello everyone, I'm using an API that describes that all API requests submitted must have the below headers:

“Content-Type”: “application/json”
“Authorization”, “Basic”, “Username:Password”
“X-Requested-With”: “XMLHttpRequest”,

Can I use a middleware to add all the headers?

0 likes
3 replies
Yorki's avatar

Are you talking about outgoing API calls from your application to external API or incoming calls to your application API? If the second one then yes, you can make middleware and add to api group checking for obligatory fields and validating them.

If you application makes API calls to external API then you should make/use API client which handles this every request. Like:

public function getUsers()
{
    return $this->get('/users');
}

protected function get($endpoint)
{
    return $this->httpClient->get($this->getFullUrl($endpoint), [
        'headers' => $this->getHeaders(),
    ]);
}

protected function getHeaders()
{
    return [
        'Authorization' => 'Basic ' . $this->authHash,
        'X-Requested-With' => 'XMLHttpRequest',
    ];
}
Conixs's avatar

It is an outgoing API calls from my API to an external API, any link about API client?

Yorki's avatar

It all depends on API, check over github if there is any ready to use api client for your external service. If not then you have to write your own from scratch. You can take a look at mine https://github.com/Yorkii/laravel-payu. You will see how I did it and used with laravel service provider.

Please or to participate in this conversation.