Tackling a Tough Backend Challenge: Integrating the AWS Seller Central API

Oahdev
3 min readJun 30, 2024

--

This experience not only put my technical skills to the test, but it also fueled my desire to solve problems and learn new things.

I was working on a project that involved connecting the AWS Seller Central API to automate the collection of orders data for analysis and reporting. On the surface, it appeared simple after all, I’d worked with APIs previously. However, I quickly realized that this integration wasn’t has I thought.

Solution Process

  1. Understanding the API
    The first step was to properly study the AWS seller central API. I read through the documentation, focusing on authentication processes and available endpoints. AWS employs signature version 4 (SigV4) for authentication, which involves creating a canonical request, string-to-sign, and signing it with the AWS secret key.
  2. Setting Up The Environment
    Next, I configured my PHP development environment. This involved installing required libraries like “GuzzleHTTP” for HTTP queries and “Aws\Sdk” for easier integration with AWS services.
require 'vendor/autoload.php';

use Aws\Sdk;
use GuzzleHttp\Client;

$client = new Client();
$sdk = new Sdk([
'region' => 'us-west-1',
'version' => 'latest'
]);

3. Authenticating Request
Authentication was the most difficult aspect. I had to write a function to build the required headers for SigV4. I used some of the codes from the AWS documentation to achieve this.

function getSignatureKey($key, $date, $region, $service) {
$kDate = hash_hmac('sha256', $date, 'AWS4' . $key, true);
$kRegion = hash_hmac('sha256', $region, $kDate, true);
$kService = hash_hmac('sha256', $service, $kRegion, true);
$kSigning = hash_hmac('sha256', 'aws4_request', $kService, true);

return $kSigning;
}

function createAuthorizationHeader($accessKey, $secretKey, $region, $service, $request) {
$date = gmdate('Ymd');
$dateTime = gmdate('Ymd\THis\Z');
$canonicalRequest = createCanonicalRequest($request);
$stringToSign = createStringToSign($dateTime, $region, $service, $canonicalRequest);
$signature = hash_hmac('sha256', $stringToSign, getSignatureKey($secretKey, $date, $region, $service));

return "AWS4-HMAC-SHA256 Credential=$accessKey/$date/$region/$service/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=$signature";
}

4. Making API calls
With authentication in place, I was able to make requests to the AWS seller central API. I created a function to handle these requests, which includes error handling for unexpected failures.

function get_orders($endpoint, $params) {
global $client;
$request = new \GuzzleHttp\Psr7\Request('GET', $endpoint, $params);

try {
$response = $client->send($request);
return json_decode($response->getBody()->getContents(), true);
} catch (Exception $e) {
echo 'Request failed: ' . $e->getMessage();
return null;
}
}

$endpoint = 'https://sellingpartnerapi-na.amazon.com/orders/v0/orders';
$params = [
'headers' => [
'Authorization' => createAuthorizationHeader($accessKey, $secretKey, $region, 'execute-api', $request),
'x-amz-date' => gmdate('Ymd\THis\Z'),
'Content-Type' => 'application/json'
]
];

$response = get_orders($endpoint, $params);

After numerous attempts and tests, I finally integrated the AWS seller central API into the PHP application. The achievement was not just a technical accomplishment, but it also proved my commitment and problem-solving skills.

As I prepare for the HNG Internship, I am excited to apply and enhance my skills in a collaborative and creative environment. The HNG Internship is well-known for its rigorous training and real-world tasks, which fully align with my career goals. I’m excited to learn from experts in the field, contribute to interesting projects, and advance as a backend developer.

This internship allows me to push myself further, build on my previous experiences, and take important steps toward a successful career in technology.

You can also participate in the HNG internship using the following links:

--

--