Search code examples
phpamazon-selling-partner-api

Amazon SP-API GetShipments ArgumentCountError


Trying to get a list of inbound shipments in status Working using the Amazon SP-API but getting the error:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function SellingPartnerApi\Api\FbaInboundV0Api::getShipments(), 1 passed

// Include Composer's autoloader for dependencies
require '../vendor/autoload.php'; 

// Load environment variables from .env file
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();

// Access environment variables
$lwaClientId = $_ENV['LWA_CLIENT_ID'];
$lwaClientSecret = $_ENV['LWA_CLIENT_SECRET'];
$refreshToken = $_ENV['LWA_REFRESH_TOKEN'];
$awsAccessKeyId = $_ENV['AWS_ACCESS_KEY_ID'];
$awsSecretAccessKey = $_ENV['AWS_SECRET_ACCESS_KEY'];
$roleArn = $_ENV['ROLE_ARN'];
$marketplaceId = $_ENV['MARKETPLACE_ID'];

use SellingPartnerApi\Api\FbaInboundV0Api;
use SellingPartnerApi\Configuration;
use SellingPartnerApi\Endpoint;        

function getUniqueSkusFromWorkingShipments($lwaClientId, $lwaClientSecret, $refreshToken, $awsAccessKeyId, $awsSecretAccessKey, $roleArn, $marketplaceId) {
        // Configure the API client
        $config = new Configuration([
            "lwaClientId" => $lwaClientId,
            "lwaClientSecret" => $lwaClientSecret,
            "lwaRefreshToken" => $refreshToken,
            "awsAccessKeyId" => $awsAccessKeyId,
            "awsSecretAccessKey" => $awsSecretAccessKey,
            "endpoint" => Endpoint::NA, // Adjust if needed
            "roleArn" => $roleArn,
        ]);
    
        $apiInstance = new FbaInboundV0Api($config);
    
        try {
            // Define the parameters
            $queryType = 'SHIPMENT'; // Required: Define the query type
            $shipmentStatusList = ['WORKING']; // Required: List of shipment statuses
            $shipmentIdList = null; // Optional: Specific shipment IDs
            $lastUpdatedAfter = null; // Optional: Date filtering
            $lastUpdatedBefore = null; // Optional: Date filtering
            $nextToken = null; // Optional: Pagination token
    
            // Call the getShipments method with the required parameters
            $response = $apiInstance->getShipments(
                $queryType, 
                $shipmentStatusList, 
                $shipmentIdList, 
                $lastUpdatedAfter, 
                $lastUpdatedBefore, 
                $nextToken, 
                $marketplaceId
            );
    
            $shipments = $response['payload']['ShipmentData'];
    
            $uniqueSkus = [];
    
            foreach ($shipments as $shipment) {
                $shipmentId = $shipment['ShipmentId'];
                $shipmentItemsResponse = $apiInstance->getShipmentItems($shipmentId);
    
                $shipmentItems = $shipmentItemsResponse['payload']['ItemData'];
    
                foreach ($shipmentItems as $item) {
                    $sku = $item['SellerSKU'];
                    $uniqueSkus[$sku] = true; // Use associative array to ensure uniqueness
                }
            }
    
            return array_keys($uniqueSkus);
        } catch (Exception $e) {
            echo 'Exception when calling FbaInboundV0Api->getShipments: ', $e->getMessage(), PHP_EOL;
            return [];
        }
    }

Alternatively I try it this way and get this error:

Exception when calling FbaInboundV0Api->getShipments: [400] { "errors": [ { "code": "InvalidInput", "message": "At least one of ShipmentStatusList and ShipmentIdList must be provided", "details": "" } ] }

function getUniqueSkusFromWorkingShipments($lwaClientId, $lwaClientSecret, $refreshToken, $awsAccessKeyId, $awsSecretAccessKey, $roleArn, $marketplaceId) {
    // Configure the API client
    $config = new Configuration([
        "lwaClientId" => $lwaClientId,
        "lwaClientSecret" => $lwaClientSecret,
        "lwaRefreshToken" => $refreshToken,
        "awsAccessKeyId" => $awsAccessKeyId,
        "awsSecretAccessKey" => $awsSecretAccessKey,
        "endpoint" => Endpoint::NA, // Adjust if needed
        "roleArn" => $roleArn,
    ]);

    $apiInstance = new FbaInboundV0Api($config);

    try {
        // Define the parameters
        $queryType = 'SHIPMENT'; // Required: Define the query type
        $shipmentStatusList = ['WORKING']; // Required: List of shipment statuses

        // Ensure ShipmentStatusList is properly formatted as an array
        $response = $apiInstance->getShipments(
            $queryType, 
            $shipmentStatusList, 
            null, // ShipmentIdList (optional, so null here)
            null, // LastUpdatedAfter (optional)
            null, // LastUpdatedBefore (optional)
            null, // NextToken (optional)
            $marketplaceId // Required: The marketplace ID
        );

        $shipments = $response['payload']['ShipmentData'];

        $uniqueSkus = [];

        foreach ($shipments as $shipment) {
            $shipmentId = $shipment['ShipmentId'];
            $shipmentItemsResponse = $apiInstance->getShipmentItems($shipmentId);

            $shipmentItems = $shipmentItemsResponse['payload']['ItemData'];

            foreach ($shipmentItems as $item) {
                $sku = $item['SellerSKU'];
                $uniqueSkus[$sku] = true; // Use associative array to ensure uniqueness
            }
        }

        return array_keys($uniqueSkus);
    } catch (Exception $e) {
        echo 'Exception when calling FbaInboundV0Api->getShipments: ', $e->getMessage(), PHP_EOL;
        return [];
    }
}

Solution

  • If the library you are using is the Amazon official one, then it seems, by looking at this example, that you are wrongly sorting arguments on the getShipments call.

    Should be something like this (note the moved marketplaceId parameter):

    $response = $apiInstance->getShipments(
                    $queryType, 
                    $marketplaceId,
                    $shipmentStatusList, 
                    $shipmentIdList, 
                    $lastUpdatedAfter, 
                    $lastUpdatedBefore, 
                    $nextToken
                );