> ## Documentation Index
> Fetch the complete documentation index at: https://documentation.bannerify.co/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP SDK

> Learn how to generate images and PDFs with the Bannerify PHP SDK.

Use `bannerify/bannerify` to call the Bannerify API from PHP without writing raw HTTP requests.

## Install

Install via Composer:

```bash theme={null}
composer require bannerify/bannerify
```

## Instantiate

You need a project API key before using the SDK. Create one in **Dashboard → API Keys** and keep it server-side.

```php theme={null}
<?php

use Bannerify\Bannerify\BannerifyClient;

$client = new BannerifyClient('bnfy_xxx');
```

## Response format

To make error handling explicit and prevent forgotten error checks, we return errors as part of the response structure. Every method returns an array with either a `result` or an `error` field, never both and never none.

<CodeGroup titles={["Success", "Error"]}>
  ```php theme={null}
  [
      'result' => mixed // the result depends on what method you called
  ]
  ```

  ```php theme={null}
  [
      'error' => [
          // A machine readable error code
          'code' => 'ERROR_CODE',
          
          // A link to our documentation explaining this error
          'docs' => 'https://bannerify.co/docs',
          
          // A human readable explanation
          'message' => 'Error message'
      ]
  ]
  ```
</CodeGroup>

## Checking for errors

To check for errors, use the `error` key in the response:

```php theme={null}
<?php

use Bannerify\Bannerify\BannerifyClient;

$client = new BannerifyClient('your-api-key');
$result = $client->createImage('tpl_xxxxxxxxx', [
    'modifications' => [
        ['name' => 'title', 'text' => 'Hello World']
    ]
]);

if (isset($result['error'])) {
    // Handle error
    echo "Error: " . $result['error']['message'];
    return;
}

// Process the result
file_put_contents('output.png', $result['result']);
```

## Options

The constructor accepts some options to customize the behavior:

### API Key

<ParamField body="apiKey" type="string" required>
  Your project’s API key
</ParamField>

```php theme={null}
$client = new BannerifyClient('my-api-key');
```

### Base URL

Override the default API base URL:

```php theme={null}
$client = new BannerifyClient('my-api-key', [
    'baseUrl' => 'https://api.bannerify.co'
]);
```

## Examples

### Generate PNG Image

```php theme={null}
$result = $client->createImage('tpl_xxxxxxxxx', [
    'modifications' => [
        ['name' => 'title', 'text' => 'Hello World'],
        ['name' => 'subtitle', 'text' => 'From PHP SDK']
    ]
]);

if (isset($result['result'])) {
    file_put_contents('output.png', $result['result']);
}
```

### Generate WebP Image

```php theme={null}
$result = $client->createImage('tpl_xxxxxxxxx', [
    'format' => 'webp',
    'modifications' => [
        ['name' => 'title', 'text' => 'Hello WebP']
    ]
]);

if (isset($result['result'])) {
    file_put_contents('output.webp', $result['result']);
}
```

### Generate Signed URL

```php theme={null}
$signedUrl = $client->generateImageSignedUrl('tpl_xxxxxxxxx', [
    'modifications' => [
        ['name' => 'title', 'text' => 'Dynamic Title']
    ]
]);

echo "<img src='{$signedUrl}' alt='Generated Image' />";
```
