Search by

konradmichalik / php-outbound

konradmichalik

Outbound - Declarative API clients for PHP: describe external APIs as interfaces with attributes, with retries, caching, rate limits and safe defaults.

Package info

github.com/konradmichalik/php-outbound

pkg:composer/konradmichalik/php-outbound

Statistics

Installs: 347

Dependents: 1

Suggesters: 0

Stars: 1

Open Issues: 0

0.1.0 2026-11-23 20:38 UTC

This package is auto-updated.

Last update: 2026-11-24 12:56:24 UTC


README

icon

php-outbound

Supported PHP Versions CGL Tests Coverage License

Declarative API clients for PHP: describe external APIs as interfaces with attributes and get the implementation generated. Your code calls methods and receives typed objects, without HTTP code of its own.

Note

The goal is to replace the hand-written service wrapper per API: you declare what an API offers, and timeouts, retries, caching and security come as safe defaults.

Outbound as in outgoing requests: your application calls the API.

Warning

This package is at an early stage. Attributes and APIs may still change before 1.0.

✨ Features

  • Less code per API: one interface method per endpoint and typed DTOs instead of request classes and HTTP plumbing
  • Fast: cached responses, revalidation with ETags and a miss lock
  • Keeps working when an API fails: short timeouts, safe retries, a circuit breaker and, if you allow it, stale data to bridge an outage
  • Secure by default: HTTPS only, allowed hosts, private networks blocked, secrets from the environment and redacted logs
  • Quick start from OpenAPI: interfaces and DTOs generated from an OpenAPI document or a sample response
  • Tests without network: mocks by endpoint instead of URL

🔥 Installation

Packagist Packagist Downloads

Requires PHP 8.2+ and Symfony components 7.4.

composer require konradmichalik/php-outbound

Using TYPO3? Install konradmichalik/typo3-outbound, which builds on this package.

🚀 Quick start

Declare the API as an interface carrying #[Connection] and annotate a method with an HTTP verb:

use KonradMichalik\Outbound\Attribute as Outbound;

#[Outbound\Connection(baseUri: 'https://api.weather.example.com')]
interface WeatherApi
{
    /** @return array{city: string, temperature: float} */
    #[Outbound\Get('/forecast/{city}')]
    public function forecast(string $city): array;
}

Compose the runtime and call the method:

use KonradMichalik\Outbound\Outbound;

$weather = Outbound::create([WeatherApi::class])->api(WeatherApi::class);

$temperature = $weather->forecast('Dresden')['temperature'];

forecast('Dresden') sends GET https://api.weather.example.com/forecast/Dresden, checks the JSON response against the declared shape and returns it. For larger responses, return a DTO class instead of an array shape.

Everything else is opt-in. A larger API splits into resources, takes its base URI and secret from the runtime configuration, maps responses to DTOs, retries, caches and maps a 404 to null, all declared with attributes:

use KonradMichalik\Outbound\Value\{Secret, Setting};

#[Outbound\Connection(baseUri: new Setting('crm.baseUri'), allowedHosts: ['crm.example.com'])]
#[Outbound\Auth\Bearer(token: new Secret('crm.token'))]
#[Outbound\Retry(maxRetries: 2)]
interface CrmApi {}

#[Outbound\Resource('/contacts')]
interface ContactsApi extends CrmApi
{
    #[Outbound\Get('/{id}')]
    #[Outbound\Cache(lifetime: 600, tags: ['crm_contact_{id}'], staleOnError: 3600)]
    #[Outbound\OnStatus(404, returns: null)]
    public function find(int $id): ?ContactDto;
}

See Defining endpoints for connections, resources, parameters and return types, and Features for every opt-in attribute.

📚 Documentation

Full documentation lives in docs/: defining endpoints, features, plain PHP usage and background.

🧑‍💻 Contributing

Please have a look at CONTRIBUTING.md.

⭐ License

This project is licensed under the MIT License.