Spryker’s Glue API is a powerful tool for exposing data to frontend applications, mobile apps, and third-party integrations. In this guide, we will walk through the process of creating a custom frontend Glue API in Spryker.
What is Spryker’s Glue API?
Spryker’s Glue API follows RESTful principles and provides scalable API solutions. The frontend Glue API allows data exposure that can be consumed by web or mobile frontends without requiring backend authentication.
Creating a Custom Glue API Module
To create a custom Glue API, follow these steps:
Step 1: Create a New Module
Navigate to the Spryker project’s src/Pyz/Glue directory and create a new module. Let’s name it CustomApi:
1 2 |
cd src/Pyz/Glue mkdir CustomApi |
Step 2: Define Your Glue API Plugin
Create a new CustomApiResourcePlugin.php inside CustomApi/Plugin/GlueApplication/:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<?php namespace Pyz\Glue\CustomApi\Plugin\GlueApplication; use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceRouteCollectionInterface; use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceRoutePluginInterface; use Spryker\Glue\Kernel\AbstractPlugin; use Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface; use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface; /** * @method \Pyz\Glue\CustomApi\CustomApiFactory getFactory() */ class CustomApiResourcePlugin extends AbstractPlugin implements ResourceRoutePluginInterface { /** * {@inheritDoc} * - Configures the /custom-api endpoint. */ public function configure(ResourceRouteCollectionInterface $resourceRouteCollection): ResourceRouteCollectionInterface { return $resourceRouteCollection->addGet('get', false ); } /** * {@inheritDoc} * - Returns a custom message. */ public function get(RestRequestInterface $restRequest): RestResponseInterface { return $this->getFactory()->createCustomApiProcessor()->createCustomMessageResponse(); } /** * {@inheritDoc} */ public function getResourceType(): string { return 'custom-api'; } /** * {@inheritDoc} * - Defines the controller for this API. */ public function getController(): string { return 'custom-api-resource'; } /** * {@inheritDoc} * - Defines the resource attributes class. */ public function getResourceAttributesClassName(): string { return ''; // FIX: Return an empty string instead of null } } ?> |
Step 3: Implement the Factory
Create a CustomApiFactory.php inside CustomApi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php namespace Pyz\Glue\CustomApi; use Spryker\Glue\Kernel\AbstractFactory; use Pyz\Glue\CustomApi\Processor\CustomApiProcessor; use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface; class CustomApiFactory extends AbstractFactory { public function createCustomApiProcessor(): CustomApiProcessor { return new CustomApiProcessor($this->getRestResourceBuilder()); } public function getRestResourceBuilder(): RestResourceBuilderInterface { return $this->getProvidedDependency(CustomApiDependencyProvider::RESOURCE_BUILDER); } } ?> |
Step 4: Implement the CustomApiDependencyProvider.php
Create a CustomApiDependencyProvider.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php namespace Pyz\Glue\CustomApi; use Spryker\Glue\Kernel\AbstractBundleDependencyProvider; use Spryker\Glue\Kernel\Container; use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilder; use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface; class CustomApiDependencyProvider extends AbstractBundleDependencyProvider { public const RESOURCE_BUILDER = 'RESOURCE_BUILDER'; public function provideDependencies(Container $container): Container { $container = parent::provideDependencies($container); $container->set(self::RESOURCE_BUILDER, function (Container $container) { return new RestResourceBuilder(); }); return $container; } } ?> |
Step 5: Implement the Controller
CustomApiResourceController.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php namespace Pyz\Glue\CustomApi\Controller; use Spryker\Glue\Kernel\Controller\AbstractController; use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface; use Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface; class CustomApiResourceController extends AbstractController { /** * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest * * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface */ public function getAction(RestRequestInterface $restRequest): RestResponseInterface { $customApiProcessor = $this->getFactory()->createCustomApiProcessor(); return $customApiProcessor->createCustomMessageResponse(); // ✅ Fixed method call } } ?> |
Step 6: Implement the Processor
CustomApiProcessor.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
<?php namespace Pyz\Glue\CustomApi\Processor; use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface; use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface; use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface; use Generated\Shared\Transfer\RestCustomApiResponseTransfer; class CustomApiProcessor { /** * @var \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface */ protected $restResourceBuilder; /** * @param \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface $restResourceBuilder */ public function __construct(RestResourceBuilderInterface $restResourceBuilder) { $this->restResourceBuilder = $restResourceBuilder; } /** * Creates a success response message. * * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface */ public function createCustomMessageResponse(): RestResponseInterface { $restResponse = $this->restResourceBuilder->createRestResponse(); // ✅ Create Transfer Object $responseTransfer = new RestCustomApiResponseTransfer(); $responseTransfer->setMessage('Success! Custom API response received.'); // ✅ Create RestResource with Transfer Object $restResource = $this->restResourceBuilder->createRestResource( 'custom-api', // Resource name null, // No ID needed $responseTransfer // ✅ Pass Transfer object instead of array ); return $restResponse->addResource($restResource); } } ?> |
Step 7: Register the Plugin
To register the plugin, modify GlueApplicationDependencyProvider.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace Pyz\Glue\GlueApplication; use Pyz\Glue\CustomApi\Plugin\GlueApplication\CustomApiResourcePlugin; use Spryker\Glue\GlueApplication\GlueApplicationDependencyProvider as SprykerGlueApplicationDependencyProvider; class GlueApplicationDependencyProvider extends SprykerGlueApplicationDependencyProvider { protected function getResourceRoutePlugins(): array { return [ new CustomApiResourcePlugin(), ]; } } ?> |
Step 8: Define the Transfer Object
Create custom_api.transfer.xml inside src/Pyz/Shared/CustomApi/Transfer/:
1 2 3 4 5 6 7 8 9 10 11 |
<transfer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://static.spryker.com/transfer.xsd"> <transfer name="RestCustomApiResponse"> <property name="message" type="string"/> </transfer> </transfer> |
Step 9: Generate Transfer Objects and Clear Cache
Run the following command to clear caches:
1 2 3 4 5 |
docker/sdk cli console transfer:generate console cache:empty-all |
Now, start your Spryker application and test the API using a tool like Postman or CURL:
GEThttp://glue.eu.spryker.local/custom-api
You should receive a JSON response:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
{ "data": [ { "type": "custom-api", "id": null, "attributes": { "message": "Success! Custom API response received." }, "links": { "self": "http://glue.eu.spryker.local/custom-api" } } ], "links": { "self": "http://glue.eu.spryker.local/custom-api" } } |
Step 10: Removing the Access Token Requirement
By default, Spryker APIs require authentication. To make this API public, update the plugin configuration:
Modify CustomApiResourcePlugin.php:
1 2 3 4 5 6 7 |
public function configure(ResourceRouteCollectionInterface $resourceRouteCollection): ResourceRouteCollectionInterface { return $resourceRouteCollection->addGet('custom-api', false); // Set to false for public access } |
Conclusion
In this guide, we created a custom frontend Glue API in Spryker, registered it, and made it accessible without an access token. This API can be extended to return dynamic data from the database or other sources.
bluethinkinc_blog
2025-03-12