Title: ApiKeyRequestAuthentication
Published: May 20, 2026

---

# class ApiKeyRequestAuthentication {}

## In this article

 * [Methods](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/?output_format=md#methods)
 * [Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/?output_format=md#source)
 * [Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/?output_format=md#wp--skip-link--target)

Class for HTTP request authentication using an API key.

## 󠀁[Methods](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/?output_format=md#methods)󠁿

| Name | Description | 
| [ApiKeyRequestAuthentication::__construct](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/__construct/) | Constructor. | 
| [ApiKeyRequestAuthentication::authenticateRequest](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/authenticaterequest/) | {@inheritDoc} | 
| [ApiKeyRequestAuthentication::fromArray](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/fromarray/) | {@inheritDoc} | 
| [ApiKeyRequestAuthentication::getApiKey](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/getapikey/) | Gets the API key. | 
| [ApiKeyRequestAuthentication::getJsonSchema](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/getjsonschema/) | {@inheritDoc} | 
| [ApiKeyRequestAuthentication::toArray](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/toarray/) | {@inheritDoc} |

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/?output_format=md#source)󠁿

    ```php
    class ApiKeyRequestAuthentication extends AbstractDataTransferObject implements RequestAuthenticationInterface
    {
        public const KEY_API_KEY = 'apiKey';
        /**
         * @var string The API key used for authentication.
         */
        protected string $apiKey;
        /**
         * Constructor.
         *
         * @since 0.1.0
         *
         * @param string $apiKey The API key used for authentication.
         */
        public function __construct(string $apiKey)
        {
            $this->apiKey = $apiKey;
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         */
        public function authenticateRequest(\WordPress\AiClient\Providers\Http\DTO\Request $request): \WordPress\AiClient\Providers\Http\DTO\Request
        {
            // Add the API key to the request headers.
            return $request->withHeader('Authorization', 'Bearer ' . $this->apiKey);
        }
        /**
         * Gets the API key.
         *
         * @since 0.1.0
         *
         * @return string The API key.
         */
        public function getApiKey(): string
        {
            return $this->apiKey;
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         *
         * @since 0.1.0
         *
         * @return ApiKeyRequestAuthenticationArrayShape
         */
        public function toArray(): array
        {
            return [self::KEY_API_KEY => $this->apiKey];
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         *
         * @since 0.1.0
         */
        public static function fromArray(array $array): self
        {
            static::validateFromArrayData($array, [self::KEY_API_KEY]);
            return new self($array[self::KEY_API_KEY]);
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         */
        public static function getJsonSchema(): array
        {
            return ['type' => 'object', 'properties' => [self::KEY_API_KEY => ['type' => 'string', 'title' => 'API Key', 'description' => 'The API key used for authentication.']], 'required' => [self::KEY_API_KEY]];
        }
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/php-ai-client/src/providers/http/dto/apikeyrequestauthentication.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/php-ai-client/src/Providers/Http/DTO/ApiKeyRequestAuthentication.php#L19)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/php-ai-client/src/Providers/Http/DTO/ApiKeyRequestAuthentication.php#L19-L92)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-apikeyrequestauthentication/?output_format=md#changelog)󠁿

| Version | Description | 
| [0.1.0](https://developer.wordpress.org/reference/since/0.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwordpress-aiclient-providers-http-dto-apikeyrequestauthentication%2F)
before being able to contribute a note or feedback.