Simplify connecting to the Selling Partner API with Laravel. Uses jlevers/selling-partner-api under the hood.
jlevers/selling-partner-api
: A PHP library for Amazon's Selling Partner API. highsidelabs/laravel-spapi
is a Laravel wrapper around jlevers/selling-partner-api
.highsidelabs/walmart-api
: A PHP library for Walmart's seller and supplier APIs, including the Marketplace, Drop Ship Vendor, Content Provider, and Warehouse Supplier APIs.highsidelabs/amazon-business-api
: A PHP library for Amazon's Business API, with a near-identical interface to jlevers/selling-partner-api
.This package is developed and maintained by Highside Labs. If you need support integrating with Amazon's (or any other e-commerce platform's) APIs, we're happy to help! Shoot us an email at hi@highsidelabs.co. We'd love to hear from you :)
If you've found any of our packages useful, please consider becoming a Sponsor, or making a donation via the button below. We appreciate any and all support you can provide!
There is a more in-depth guide to using this package on our blog.
$ composer require highsidelabs/laravel-spapi
This library has two modes:
$ php artisan vendor:publish --provider="HighsideLabs\LaravelSpApi\SellingPartnerApiServiceProvider" --tag="config"
.env
:SPAPI_AWS_ACCESS_KEY_ID=
SPAPI_AWS_SECRET_ACCESS_KEY=
SPAPI_LWA_CLIENT_ID=
SPAPI_LWA_CLIENT_SECRET=
SPAPI_LWA_REFRESH_TOKEN=
# Optional
# SPAPI_AWS_ROLE_ARN=
# SPAPI_ENDPOINT_REGION=
If in Seller Central, you configured your SP API app with an IAM role ARN rather than an IAM user ARN, you'll need to put that ARN in the SPAPI_AWS_ROLE_ARN
environment variable. Otherwise, you can leave it blank. Set SPAPI_ENDPOINT_REGION
to the region code for the endpoint you want to use (EU for Europe, FE for Far East, or NA for North America).
You're ready to go!
All of the API classes supported by jlevers/selling-partner-api can be type-hinted. This example assumes you have access to the Selling Partner Insights
role in your SP API app configuration (so that you can call SellersV1Api::getMarketplaceParticipations()
), but the same principle applies to type-hinting any other Selling Partner API class.
use Illuminate\Http\JsonResponse;
use SellingPartnerApi\Api\SellersV1Api as SellersApi;
use SellingPartnerApi\ApiException;
class SpApiController extends Controller
{
public function index(SellersApi $api): JsonResponse
{
try {
$result = $api->getMarketplaceParticipations();
return response()->json($result);
} catch (ApiException $e) {
$jsonBody = json_decode($e->getResponseBody());
return response()->json($jsonBody, $e->getCode());
}
}
}
# Publish config/spapi.php file
$ php artisan vendor:publish --provider="HighsideLabs\LaravelSpApi\SellingPartnerApiServiceProvider" --tag="config"
Update the configuration to support multi-seller usage.
installation_type
in config/spapi.php
to multi
.aws.dynamic
key to true. If you don't make that change before running migrations (the next step), the fields for AWS credentials won't be added to the database. (If you're not sure if this change applies to you, it probably doesn't.)Publish the multi-seller migrations:
# Publish migrations to database/migrations/
$ php artisan vendor:publish --provider="HighsideLabs\LaravelSpApi\SellingPartnerApiServiceProvider" --tag="multi"
spapi_sellers
and spapi_credentials
tables (corresponding to the HighsideLabs\LaravelSpApi\Models\Seller
and HighsideLabs\LaravelSpApi\Models\Credentials
models, respectively):$ php artisan migrate
.env
(unless you changed the aws.dynamic
configuration flag to true
in step 2):SPAPI_AWS_ACCESS_KEY_ID=
SPAPI_AWS_SECRET_ACCESS_KEY=
First you'll need to create a Seller
, and some Credentials
for that seller. The Seller
and Credentials
models work just like any other Laravel model.
use HighsideLabs\LaravelSpApi\Models;
$seller = Models\Seller::create(['name' => 'MySeller']);
$credentials = Models\Credentials::create([
'seller_id' => $seller->id,
// You can find your selling partner ID/merchant ID by going to
// https://<regional-seller-central-domain>/sw/AccountInfo/MerchantToken/step/MerchantToken
'selling_partner_id' => '<AMAZON SELLER ID>',
// Can be NA, EU, or FE
'region' => 'NA',
// The LWA client ID and client secret for the SP API application these credentials were created with
'client_id' => 'amzn....',
'client_secret' => 'fec9/aw....',
// The LWA refresh token for this seller
'refresh_token' => 'IWeB|....',
// If you have the `aws.dynamic` config flag set to true, you'll also need these attributes:
// 'access_key_id' => 'AKIA....',
// 'secret_access_key' => '23pasdf....',
// // Only necessary if you configured your SP API setup with an IAM role ARN, otherwise can be omitted
// // 'role_arn' => 'arn:aws:iam::....',
]);
Once you have credentials in the database, you can use them like this:
use HighsideLabs\LaravelSpApi\Models\Credentials;
use Illuminate\Http\JsonResponse;
use SellingPartnerApi\Api\SellersV1Api as SellersApi;
use SellingPartnerApi\ApiException;
class SpApiController extends Controller
{
public function __construct(SellersApi $api)
{
// Retrieve the credentials we just created
$creds = Credentials::first();
$this->api = $creds->useOn($api);
// You can now make calls to the SP API with $creds using $this->api!
}
public function index(): JsonResponse
{
try {
$result = $this->api->getMarketplaceParticipations();
return response()->json($result);
} catch (ApiException $e) {
$jsonBody = json_decode($e->getResponseBody());
return response()->json($jsonBody, $e->getCode());
}
}
}
Or, if you want to use a Selling Partner API class without auto-injecting it, you can quickly create one like this:
use HighsideLabs\LaravelSpApi\SellingPartnerApi;
use SellingPartnerApi\Api\SellersV1Api as SellersApi;
$creds = Credentials::first();
$api = SellingPartnerApi::makeApi(SellersApi::class, $creds);