Get Started with PHP SDK and Live Preview

This guide will help you get started with Contentstack PHP SDK to build apps powered by Contentstack.


To get started with PHP, you will need the following:

SDK Installation and Setup

To install the PHP SDK, choose either of the following methods:

Method 1: Using Composer

To install the PHP SDK in your project using Composer, fire up the terminal, point it to the project location, and run the following command:

composer require contentstack/contentstack

Method 2: Downloading the zip file

To download the PHP SDK, perform the following steps:

  1. Download the PHP SDK.
  2. Create the dependencies folder in your project directory and move the downloaded .zip file within the dependencies folder.
  3. Download the MabeEnum class.
  4. Create a folder named marc-mabe folder inside dependencies, and move the php-enum folder to marc-mabe.

Let's get started with the implementation.

Initializing the Stack with Live Preview

Since the Live Preview Utils SDK is responsible for communication, you need to initialize it within your stack.

Use the following command to initialize the stack:

$stack = Contentstack::Stack('API_KEY', 'DELIVERY_TOKEN', 'ENVIRONMENT', 
          'live_preview'=> array( 

'management_token' => 'MANAGEMENT_TOKEN', 
'enable' => true, 
'host' => '', 

Note: By default, the host parameter points to the North America endpoint. If your website is hosted on the European data center, then pass the European endpoint against the host parameter.

Create the Contentstack Service

You need to add a Stack.php service file within the src/Service file path. Within this service file, use the following code to create the Contentstack service:



namespace App\Service;

use Contentstack\Contentstack;

class Stack {
   public $client;

   public function __construct() {
       $this->client = Contentstack::Stack('API_KEY', 'DELIVERY_TOKEN', 'ENVIRONMENT', array( 'live_preview' => array('enable' => true, 'management_token' => 'MANAGEMENT_TOKEN')));

To add the Stack service, open config/services.yaml and add the following code:

. . .
         factory: ['@App\Service\Stack', 'client']
 . . .

Add Custom Middleware

You need to add a custom middleware in the ContentstackEventListener.php file.

Use the following code to get the Live Preview hash key:


namespace App\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use App\Service\Stack;
use Contentstack\Contentstack;
use Contentstack\Stack\Service;
class ContentstackRequestListener implements EventSubscriberInterface

   protected $stack;

   public function __construct(
       Stack $stack
   ) {
       $this->stack = $stack->client;

   public function onKernelRequest(RequestEvent $event)
       if (!$event->isMainRequest()) {
       $request = $event->getRequest();

       if ($request::getTrustedProxies()) {


   public static function getSubscribedEvents(): array
       return [
           KernelEvents::REQUEST => [
               ['onKernelRequest', 256],

Open the services.yaml file and add the following service:

            - { name: kernel.event_listener, event: kernel.request }

Run the following command to check whether ContentstackRequestListener has been added to the kernel request:

$ php bin/console debug:event-dispatcher kernel.request

For Server-side Rendered Websites

To install and initialize the Live Preview Utils SDK, open base.html.twig and add the following code:

   <script src=""></script>

         {% block javascripts %}
            {{ encore_entry_script_tags('app') }}

                    enable: true,
                    stackDetails: {
                        apiKey: "API_KEY",
        {% endblock %}

Note: To learn how to install Encore, you can refer to the Installing Encore documentation.

Query Request

Contentstack SDKs let you interact with the Content Delivery APIs and retrieve content from Contentstack. They are read-only in nature. The SDKs fetch and deliver content from the nearest server via Fastly, our powerful and robust CDN.

To get an entry, you need to specify the content type UID and the UID of the entry.


More Resources

Was this article helpful?

Thanks for your feedbackSmile-icon

On This Page