# PHP SDK

> Install with composer require rishimohan/orshot, then call renderFromTemplate() with a template ID and modifications.

- **URL**: https://orshot.com/docs/sdks/php

---

## Installation

```bash tab="composer"
composer require rishimohan/orshot
```

## Initialise

```php
<?php
require 'vendor/autoload.php';
use Orshot\Client;

$client = new Client("YOUR_ORSHOT_API_KEY");
```

## renderFromStudioTemplate

Render from a custom [Studio template](https://orshot.com/features/orshot-studio). Supports image, PDF, video generation and publishing to social accounts.

### Generate Image

  **Request**
```php
$response = $client->renderFromStudioTemplate([
  'templateId' => 1234,
  'modifications' => [
    'title' => 'Orshot Studio',
    'description' => 'Generate images from custom templates',
  ],
  'response' => [ 'type' => 'url', 'format' => 'png', 'scale' => 2 ],
]);
```

  **Response**
```js
{
  "data": {
    "content": "https://storage.orshot.com/.../image.png",
    "format": "png",
    "type": "url",
    "responseTime": 3550.43
  }
}
```

### Generate PDF

```php
$response = $client->renderFromStudioTemplate([
  'templateId' => 1234,
  'modifications' => [ 'title' => 'Invoice #1234' ],
  'response' => [ 'type' => 'url', 'format' => 'pdf' ],
  'pdfOptions' => [
    'margin' => '20px',
    'rangeFrom' => 1,
    'rangeTo' => 2,
    'colorMode' => 'rgb',
    'dpi' => 300,
  ],
]);
```

### Generate Video

```php
$response = $client->renderFromStudioTemplate([
  'templateId' => 1234,
  'modifications' => [
    'videoElement' => 'https://example.com/custom-video.mp4',
    'videoElement.trimStart' => 0,
    'videoElement.trimEnd' => 10,
  ],
  'response' => [ 'type' => 'url', 'format' => 'mp4' ],
  'videoOptions' => [
    'trimStart' => 0,
    'trimEnd' => 20,
    'muted' => false,
    'loop' => true,
    'fps' => 30,
    'audioSource' => 'https://example.com/audio.mp3',
  ],
]);
```

### Publish to Social Accounts

```php
$response = $client->renderFromStudioTemplate([
  'templateId' => 1234,
  'modifications' => [ 'title' => 'Check out our latest update!' ],
  'response' => [ 'type' => 'url', 'format' => 'png' ],
  'publish' => [
    'accounts' => [1, 2],
    'content' => 'Check out our latest design!',
  ],
]);
// $response['publish'] => [[ 'platform' => 'twitter', 'username' => 'acmehq', 'status' => 'published' ], ...]
```

### Schedule a Post

```php
$response = $client->renderFromStudioTemplate([
  'templateId' => 1234,
  'modifications' => [ 'title' => 'Scheduled post' ],
  'response' => [ 'type' => 'url', 'format' => 'png' ],
  'publish' => [
    'accounts' => [1],
    'content' => 'This will be posted later!',
    'schedule' => [ 'scheduledFor' => '2026-04-01T10:00:00Z' ],
    'timezone' => 'America/New_York',
  ],
]);
```

### Parameters

| key                             | required | description                                                                              |
| ------------------------------- | -------- | ---------------------------------------------------------------------------------------- |
| `templateId`                    | Yes      | ID of the Studio template.                                                               |
| `modifications`                 | No       | Array of dynamic modifications for the template.                                         |
| `response.type`                 | No       | `base64`, `binary`, `url` (Defaults to `url`).                                           |
| `response.format`               | No       | `png`, `webp`, `jpg`, `jpeg`, `avif`, `pdf`, `mp4`, `webm`, `mov`, `mkv`, `gif` (Defaults to `png`).   |
| `response.scale`                | No       | Scale of the output (`1` = original, `2` = double). Defaults to `1`.                     |
| `response.includePages`         | No       | Page numbers to render for multi-page templates (e.g. `[1, 3]`).                         |
| `response.fileName`             | No       | Custom file name (without extension). Works with `url` and `binary` types.               |
| `pdfOptions.margin`             | No       | CSS margin value (e.g. `"20px"`).                                                        |
| `pdfOptions.rangeFrom`          | No       | Start page number for PDF output.                                                        |
| `pdfOptions.rangeTo`            | No       | End page number for PDF output.                                                          |
| `pdfOptions.colorMode`          | No       | `rgb` or `cmyk`.                                                                         |
| `pdfOptions.dpi`                | No       | DPI for print quality (e.g. `300`).                                                      |
| `videoOptions.trimStart`        | No       | Trim start time in seconds.                                                              |
| `videoOptions.trimEnd`          | No       | Trim end time in seconds.                                                                |
| `videoOptions.muted`            | No       | Mute audio track.                                                                        |
| `videoOptions.loop`             | No       | Loop the video.                                                                          |
| `videoOptions.duration`         | No       | Total video duration in seconds.                                                         |
| `videoOptions.fps`              | No       | Frames per second (`1`-`30`).                                                            |
| `videoOptions.quality`          | No       | Video quality (`1`-`100`).                                                               |
| `videoOptions.audioSource`      | No       | External audio URL or array of per-page audio sources.                                   |
| `videoOptions.subtitleSource`   | No       | Subtitle file URL.                                                                       |
| `videoOptions.combinePages`     | No       | Combine multi-page templates into a single video.                                        |
| `videoOptions.pageTransition`   | No       | Transition effect between pages, for example `"fade"`, `"dissolve"`, `"wipeleft"` or `"slideup"`. An unknown name gives no transition. |
| `videoOptions.pageTransitionDuration` | No | Transition duration in seconds (`0.1`-`2`).                                              |
| `publish.accounts`              | Yes, when `publish` is set | Array of social account IDs from your workspace.                                         |
| `publish.content`               | No       | Caption/text for the social post.                                                        |
| `publish.isDraft`               | No       | `true` to save as draft instead of publishing.                                           |
| `publish.schedule.scheduledFor` | No       | ISO date string to schedule the post.                                                    |
| `publish.timezone`              | No       | Timezone string (e.g. `"America/New_York"`).                                             |
| `publish.platformOptions`       | No       | Per-account options keyed by account ID.                                                 |
| `publish.tiktokSettings`        | No       | TikTok options for the whole post. Use `[ 'autoAddMusic' => true ]` to add recommended music to TikTok photo carousels. |

---

## renderFromTemplate

Render from a pre-built Orshot template.

  **Request**
```php
$response = $client->renderFromTemplate([
  'templateId' => 'open-graph-image-1',
  'modifications' => [
    'title' => 'Orshot',
    'description' => 'Create Visuals and Automate Image Generation',
  ],
  'responseType' => 'url',
  'responseFormat' => 'png',
]);
```

  **Response**
```js
{
  "data": {
    "content": "https://storage.orshot.com/.../image.png",
    "format": "png",
    "type": "url",
    "responseTime": 3550.43
  }
}
```

| key              | required | description                                                                              |
| ---------------- | -------- | ---------------------------------------------------------------------------------------- |
| `templateId`     | Yes      | ID of the template (`open-graph-image-1`, `tweet-image`, `beautify-screenshot`, ...)     |
| `modifications`  | Yes      | Modifications for the selected template.                                                 |
| `responseType`   | No       | `base64`, `binary`, `url` (Defaults to `url`).                                           |
| `responseFormat` | No       | `png`, `webp`, `pdf`, `jpg`, `jpeg`, `avif` (Defaults to `png`).                         |

For available templates and their modifications refer [Orshot Templates Page](https://orshot.com/templates)

---

## generateSignedUrl

Generate a signed URL for a library template. Opening the URL renders the image.

  **Request**
```php
$response = $client->generateSignedUrl([
  'templateId' => 'open-graph-image-1',
  'modifications' => [ 'title' => 'Hello World' ],
  'expiresAt' => 1744276943,
]);
```

  **Response**
```js
{
  "data": {
    "url": "https://api.orshot.com/v1/generate/images?templateId=open-graph-image-1&expiresAt=1744276943&..."
  }
}
```

| key              | required | description                                                                              |
| ---------------- | -------- | ---------------------------------------------------------------------------------------- |
| `templateId`     | Yes      | ID of the template (`open-graph-image-1`, `tweet-image`, `beautify-screenshot`, ...)     |
| `modifications`  | Yes      | Modifications for the selected template.                                                 |
| `expiresAt`      | Yes      | Unix timestamp in seconds, or `"never"` for a URL that does not expire.                  |
| `renderType`     | No       | Only `images` works. Leave it out to use `images`.                                       |

The URL renders a PNG by default. To get another format, add `responseFormat` inside `modifications`, for example `'modifications' => [ 'title' => 'Hello World', 'responseFormat' => 'jpg' ]`.

---

## Error Handling

The SDK uses Guzzle. A failed request throws a Guzzle exception. The API's error text is in the response body.

```php
use GuzzleHttp\Exception\RequestException;

try {
  $response = $client->renderFromStudioTemplate([
    'templateId' => 1234,
    'modifications' => [ 'title' => 'Hello' ],
  ]);
} catch (RequestException $e) {
  if ($e->hasResponse()) {
    echo $e->getResponse()->getBody();
  }
}
```