Custom API to Fetch Customer Data by Customer ID in Spryker

Blog
Spread the love

Introduction

In this blog post, we will walk through how to build a custom Customer API in Spryker, utilizing Glue and RESTful principles to retrieve customer data in a structured way. We’ll break down each component of the code and explain its functionality, showing you how to implement a Customer API that integrates seamlessly into Spryker’s architecture.

Setting the Scene: What Are We Building?

The aim of this API is to expose customer details to external systems in a standardized JSON API format. This guide will walk through the steps, showing how to integrate customer data, implement the response structure, and handle API requests effectively.

Step 1: Define the Factory (CustomCustomerApiFactory.php)

A key component of any Spryker module is the factory, which manages the creation of dependencies. In our case, the CustomCustomerApiFactory class will handle the dependencies related to the customer API and provide a mechanism to fetch data.

Here’s a breakdown of the methods:

  • getRestResourceBuilder(): This method provides the REST resource builder required for generating the response format.
  • getCustomerClient(): This method returns an instance of the CustomerClient, allowing us to fetch customer data using the findCustomerById method.

Step 2: Managing Dependencies (CustomCustomerApiDependencyProvider.php)

Dependencies in Spryker are managed using the DependencyProvider class. This class is used to bind the dependencies that the factory will use

  • RESOURCE_BUILDER: We define this constant to point to the GlueApplicationDependencyProvider::RESOURCE_BUILDER.
  • CLIENT_CUSTOMER: This dependency provides the customer client, allowing us to interact with customer data.

Step 3: Configuring Resource Routes (CustomCustomerApiResourcePlugin.php)

In Spryker, plugins are used to extend functionality.The CustomCustomerApiResourcePlugin defines the route and resources exposed by the API. Here, we’re setting up a GET endpoint that fetches customer data.

  • configure(): Configures the route for the resource. We’re setting up a GET method.
  • getResourceType(): Defines the resource type, which is “customcustomerapi”.
  • getController(): The controller that will handle the API request.

Step 4: Fetching Customer Data (CustomCustomerApiResourceController.php)

The controller is the heart of the logic behind our API. In the getAction() method, we retrieve the customer’s ID from the request, fetch the data from the customer client, and prepare the response.

  • If the customer ID is missing or invalid, we return an error response.
  • Once the customer is found, we return their data in a structured RestCustomCustomerApiAttributesTransfer object.

Step 5: Building the Response (CustomCustomerApiResponseBuilder.php)

The CustomCustomerApiResponseBuilder is responsible for creating responses in the expected format. It builds both success and error responses using the appropriate RestResource and RestErrorMessageTransfer.

  • Success Response: Creates a RestResponse with customer attributes.
  • Error Response: Handles error scenarios by returning the appropriate status code and message.

Step 6: Defining the Transfer Object (custom_customer_api.transfer.xml)

Finally, the transfer object is defined in XML. The RestCustomCustomerApiAttributesTransfer contains the attributes we want to expose for each customer, such as idCustomer, firstName, lastName, and so on.

src/Pyz/Shared/CustomCustomerApi/Transfer/custom_customer_api.transfer.xml

Step 7: Registering the Plugin (GlueApplicationDependencyProvider.php)

Lastly, we register our resource plugin within the GlueApplicationDependencyProvider to ensure it’s included in the application’s resource routes.

To register the plugin, modify GlueApplicationDependencyProvider.php:

src/Pyz/Glue/GlueApplication/GlueApplicationDependencyProvider.php

Step 8: Generate Transfer Objects and Clear Cache

Run the following command to clear caches:

docker/sdk cli

console transfer:generate

console cache:empty-all

img

Step 9: call access tokens

Api Endpoint:

http://glue.eu.spryker.local/access-tokens

Method:

POST

Request body

Responce:

img

Get Customer data Customer id based through api

Api Endpoint:

http://glue.eu.spryker.local/customcustomerapi/?customerId=27

Authorization: Add headers for any necessary API key or token-based authentication.

Customer ID: The customer ID is passed as part of the URL to fetch data specific to that customer

Method:

GET

Response

img

Conclusion

With this step-by-step guide, you have now built a custom Spryker API to retrieve customer data based on a customer ID.You can customize it further by adding more fields, improving error handling, and securing the API as needed.

bluethinkinc_blog

bluethinkinc_blog

2025-03-28

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

Custom API to Fetch Customer Data by Customer ID in Spryker

Spread the love Introduction In this blog post, we will

Creating a Custom Module for a Custom Frontend Page in Spryker

Spread the love To create a custom module for a custom

How to create custom module api get category list without access token in Spryker

Spread the love To create a custom API for fetching a

Creating a Custom Glue API in Spryker

Spread the love Spryker’s Glue API is a powerful tool

Get AI Generated 360-Degree View of Account Summary in Dynamics 365 Sales

Spread the love Navigating the demanding landscape of sales

Enhancing Productivity with Dynamics 365: The Power of Session Restore

Spread the love Enhancing Productivity with Dynamics 365:

Salesforce Admin

Spread the love In our last blog, we discussed common

Unlocking AI with Spring Boot: Real-World Use Cases and Solutions

Spread the love Artificial Intelligence (AI) has moved from

Enhancing Performance with Reactive Java: A Beginner-Friendly Guide

Spread the love In today’s fast-paced software

Integrating Azure Function with Dynamics 365

Spread the love Signup on azure portal and make sure to

bluethinkinc Blogs