1. Go to this page and download the library: Download sashalenz/ebay-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sashalenz / ebay-api example snippets
use Sashalenz\EbayApi\Client\Auth\UserToken;
use Sashalenz\EbayApi\Client\EbayClient;
// After obtaining user's OAuth token through authorization flow:
$userToken = new UserToken(
appId: config('ebay-api.app_id'),
certId: config('ebay-api.cert_id'),
environment: \Sashalenz\EbayApi\Enums\Environment::from(config('ebay-api.environment')),
accessToken: $userAccessToken,
refreshToken: $userRefreshToken,
expiresAt: $expiresAt, // Unix timestamp
cacheKey: 'ebay_user_token_'.$userId // Optional: for caching
);
// Use with client
$client = app(EbayClient::class);
$client->setUserToken($userToken);
// Now you can call Post-Order API methods
$return = GetReturnRequest::make($returnId)->asData();
// Clear user token when done (reverts to Application Token)
$client->clearUserToken();
use Sashalenz\EbayApi\Requests\Sell\Inventory\GetInventoryItemRequest;
// Client is automatically injected via Laravel Container
$item = GetInventoryItemRequest::make('SKU123')->asData();
// Type-safe access to properties
echo $item->sku; // 'SKU123'
echo $item->product?->title; // Product title
echo $item->condition; // 'NEW'
use Sashalenz\EbayApi\Facades\EbayApi;
use Sashalenz\EbayApi\Requests\Sell\Inventory\GetInventoryItemRequest;
// Get an inventory item
$request = new GetInventoryItemRequest(app(EbayApi::class), 'SKU123');
$response = $request->send();
if ($response->successful()) {
$data = $response->json();
// Process the data
}
use Sashalenz\EbayApi\Client\EbayClient;
use Sashalenz\EbayApi\Requests\Sell\Inventory\GetInventoryItemsRequest;
class InventoryService
{
public function __construct(private EbayClient $client)
{
}
public function getAllItems()
{
$request = new GetInventoryItemsRequest($this->client);
$request->limit(50)->offset(0);
$response = $request->send();
return $response->json();
}
}
use Sashalenz\EbayApi\Requests\Sell\Inventory\InventoryItem\CreateOrReplaceInventoryItemRequest;
use Sashalenz\EbayApi\Data\Sell\Inventory\ProductData;
use Sashalenz\EbayApi\Data\Sell\Inventory\AvailabilityData;
use Sashalenz\EbayApi\Data\Sell\Inventory\ShipToLocationAvailabilityData;
use Sashalenz\EbayApi\Enums\Condition;
use Sashalenz\EbayApi\Enums\Locale;
// Using Builder Pattern with Data objects and Enums
$response = CreateOrReplaceInventoryItemRequest::make('YOUR-SKU')
->product(ProductData::from([
'title' => 'Apple iPhone 15 Pro',
'description' => 'Brand new Apple iPhone 15 Pro with 256GB storage',
'brand' => 'Apple',
'mpn' => 'MTUW3',
'aspects' => [
'Brand' => ['Apple'],
'Model' => ['iPhone 15 Pro'],
'Storage Capacity' => ['256 GB'],
],
'imageUrls' => ['https://example.com/image1.jpg'],
]))
->condition(Condition::NEW)
->locale(Locale::EN_US)
->availability(AvailabilityData::from([
'shipToLocationAvailability' => ShipToLocationAvailabilityData::from([
'quantity' => 10,
]),
]))
->send();
if ($response->successful()) {
echo "Inventory item created/updated successfully";
}
use Sashalenz\EbayApi\Requests\Sell\Inventory\InventoryItem\DeleteInventoryItemRequest;
// Delete inventory item (also deletes unpublished offers and listings)
$response = DeleteInventoryItemRequest::make('YOUR-SKU')->send();
if ($response->successful()) {
echo "Inventory item deleted successfully";
}
use Sashalenz\EbayApi\Requests\Sell\Inventory\Offer\CreateOfferRequest;
use Sashalenz\EbayApi\Data\Sell\Inventory\PricingSummaryData;
use Sashalenz\EbayApi\Data\Sell\Inventory\AmountData;
use Sashalenz\EbayApi\Data\Sell\Inventory\ListingPoliciesData;
use Sashalenz\EbayApi\Enums\MarketplaceId;
$offer = CreateOfferRequest::make('YOUR-SKU', MarketplaceId::EBAY_US, 'FIXED_PRICE')
->categoryId('139971')
->pricingSummary(PricingSummaryData::from([
'price' => AmountData::from([
'currency' => 'USD',
'value' => '999.99',
]),
]))
->listingPolicies(ListingPoliciesData::from([
'fulfillmentPolicyId' => 'policy-123',
'paymentPolicyId' => 'policy-456',
'returnPolicyId' => 'policy-789',
]))
->quantityLimitPerBuyer(5)
->merchantLocationKey('warehouse-01')
->asData();
echo "Offer ID: {$offer->offerId}";
use Sashalenz\EbayApi\Requests\Sell\Inventory\InventoryItem\BulkUpdatePriceQuantityRequest;
use Sashalenz\EbayApi\Data\Sell\Inventory\AvailabilityData;
use Sashalenz\EbayApi\Data\Sell\Inventory\PricingSummaryData;
// Update price and quantity for multiple items
$response = BulkUpdatePriceQuantityRequest::make()
->addRequest(
'SKU-001',
AvailabilityData::from(['shipToLocationAvailability' => ['quantity' => 50]]),
PricingSummaryData::from(['price' => ['value' => '29.99', 'currency' => 'USD']])
)
->addRequest(
'SKU-002',
AvailabilityData::from(['shipToLocationAvailability' => ['quantity' => 100]]),
PricingSummaryData::from(['price' => ['value' => '49.99', 'currency' => 'USD']])
)
->send();
use Sashalenz\EbayApi\Requests\Commerce\Taxonomy\CategoryTree\GetDefaultCategoryTreeIdRequest;
use Sashalenz\EbayApi\Enums\MarketplaceId;
// Get default category tree for US marketplace
$tree = GetDefaultCategoryTreeIdRequest::make(MarketplaceId::EBAY_US)->asData();
echo "Tree ID: {$tree->categoryTreeId}" . PHP_EOL;
echo "Version: {$tree->categoryTreeVersion}" . PHP_EOL;
// For different marketplaces
$ukTree = GetDefaultCategoryTreeIdRequest::make(MarketplaceId::EBAY_GB)->asData();
$deTree = GetDefaultCategoryTreeIdRequest::make(MarketplaceId::EBAY_DE)->asData();
use Sashalenz\EbayApi\Requests\Commerce\Taxonomy\CategoryTree\GetItemAspectsForCategoryRequest;
// Get aspects for a specific leaf category
$metadata = GetItemAspectsForCategoryRequest::make('0', '178090')->asData();
foreach ($metadata->aspects ?? [] as $aspect) {
echo "Aspect: {$aspect->localizedAspectName}";
if ($aspect->aspectConstraint?->aspectRequired) {
echo " [Required]";
}
echo " ({$aspect->aspectConstraint?->aspectUsage?->value})" . PHP_EOL;
// Available values
if ($aspect->aspectValues !== null) {
$values = $aspect->aspectValues->pluck('localizedValue')->take(3)->toArray();
echo " Values: " . implode(', ', $values) . PHP_EOL;
}
}
use Sashalenz\EbayApi\Requests\Commerce\Taxonomy\CategoryTree\GetCompatibilityPropertiesRequest;
// Get compatible vehicle properties for a parts category
$response = GetCompatibilityPropertiesRequest::make('100', '33733')->asData();
foreach ($response->compatibilityProperties ?? [] as $property) {
echo "Property: {$property->name} (Localized: {$property->localizedName})" . PHP_EOL;
}
// Output: Year, Make, Model, Trim, Engine
use Sashalenz\EbayApi\Requests\Commerce\Taxonomy\CategoryTree\GetCompatibilityPropertyValuesRequest;
// Get all 2018 Honda models
$models = GetCompatibilityPropertyValuesRequest::make('100', '33559', 'Model')
->filter('Year', '2018')
->filter('Make', 'Honda')
->asData();
foreach ($models->compatibilityPropertyValues ?? [] as $model) {
echo "Model: {$model->value}" . PHP_EOL;
}
// Or use filters() method
$trims = GetCompatibilityPropertyValuesRequest::make('100', '6030', 'Trim')
->filters([
'Year' => '2018',
'Make' => 'Ferrari',
])
->asData();
foreach ($trims->compatibilityPropertyValues ?? [] as $trim) {
echo "Trim: {$trim->value}" . PHP_EOL;
}
use Sashalenz\EbayApi\Requests\Commerce\Taxonomy\CategoryTree\GetExpiredCategoriesRequest;
// Get mappings of expired categories to active replacements
$response = GetExpiredCategoriesRequest::make('0')->asData();
foreach ($response->expiredCategories ?? [] as $mapping) {
echo "Expired: {$mapping->fromCategoryId} -> Active: {$mapping->toCategoryId}" . PHP_EOL;
}
use Sashalenz\EbayApi\Requests\Commerce\Taxonomy\CategoryTree\FetchItemAspectsRequest;
// Get all aspects for all leaf categories (large response, gzipped)
$response = FetchItemAspectsRequest::make('0')->asData();
echo "Tree ID: {$response->categoryTreeId}" . PHP_EOL;
echo "Version: {$response->categoryTreeVersion}" . PHP_EOL;
echo "Categories with aspects: {$response->categoryAspects?->count()}" . PHP_EOL;
// Iterate through category aspects
foreach ($response->categoryAspects ?? [] as $categoryAspect) {
echo "Category: {$categoryAspect->category?->categoryName}" . PHP_EOL;
foreach ($categoryAspect->aspects ?? [] as $aspect) {
echo " - {$aspect->localizedAspectName}";
if ($aspect->aspectConstraint?->aspectRequired) {
echo " (Required)";
}
echo PHP_EOL;
}
}
namespace App\Listeners;
use Sashalenz\EbayApi\Events\Notifications\ItemSoldEvent;
use Illuminate\Support\Facades\Log;
class HandleItemSold
{
public function handle(ItemSoldEvent $event): void
{
Log::info('Item sold on eBay', [
'item_id' => $event->itemId,
'transaction_id' => $event->transactionId,
'buyer' => $event->buyerUserId,
'quantity' => $event->quantityPurchased,
'price' => $event->transactionPrice,
]);
// Update your inventory
// Send confirmation email
// Create shipping label
// etc.
}
}
use Sashalenz\EbayApi\Events\Notifications\MarketplaceAccountDeletionEvent;
class DeleteUserData
{
public function handle(MarketplaceAccountDeletionEvent $event): void
{
// Delete or anonymize user data
User::where('ebay_user_id', $event->userId)->delete();
Log::info('User data deleted for GDPR compliance', [
'user_id' => $event->userId,
'deletion_date' => $event->deletionDate,
]);
}
}
use Sashalenz\EbayApi\Models\EbayNotification;
// Get unprocessed notifications
$pending = EbayNotification::unprocessed()->get();
// Get failed notifications
$failed = EbayNotification::failed()->get();
// Get notifications by event type
$itemSoldNotifications = EbayNotification::byEventName('ItemSold')->get();
// Get notifications for specific user
$userNotifications = EbayNotification::byRecipient('user123')->get();
use Sashalenz\EbayApi\Jobs\ProcessEbayNotificationJob;
$failedNotifications = EbayNotification::failed()->get();
foreach ($failedNotifications as $notification) {
ProcessEbayNotificationJob::dispatch($notification->id);
}
namespace App\Listeners;
use Sashalenz\EbayApi\Events\Notifications\MarketplaceAccountDeletionNotificationEvent;
use Illuminate\Support\Facades\Log;
use App\Models\User;
class DeleteUserEbayData
{
public function handle(MarketplaceAccountDeletionNotificationEvent $event): void
{
$username = $event->getUsername();
$userId = $event->getUserId();
// Find user by eBay user ID
$user = User::where('ebay_user_id', $userId)->first();
if ($user) {
// Delete or anonymize all eBay-related data
$user->orders()->where('source', 'ebay')->delete();
$user->ebay_listings()->delete();
$user->ebay_transactions()->delete();
// Anonymize user data
$user->update([
'ebay_user_id' => null,
'ebay_username' => null,
'ebay_access_token' => null,
'ebay_refresh_token' => null,
]);
Log::info('User eBay data deleted for GDPR compliance', [
'username' => $username,
'user_id' => $userId,
'notification_id' => $event->notification->notification_id,
]);
}
}
}
use Sashalenz\EbayApi\Jobs\ProcessMarketplaceAccountDeletionJob;
// Manually dispatch job
ProcessMarketplaceAccountDeletionJob::dispatch($notificationId);
// Check queue status
php artisan queue:work --queue=default
use Sashalenz\EbayApi\Client\Auth\UserToken;
use Sashalenz\EbayApi\Requests\PostOrder\Return\CreateReturnRequest;
use Sashalenz\EbayApi\Requests\PostOrder\Cancellation\CreateCancellationRequest;
// Create user token
$userToken = new UserToken(
appId: config('ebay-api.app_id'),
certId: config('ebay-api.cert_id'),
environment: \Sashalenz\EbayApi\Enums\Environment::from(config('ebay-api.environment')),
accessToken: $user->ebay_access_token,
refreshToken: $user->ebay_refresh_token,
expiresAt: $user->ebay_token_expires_at,
cacheKey: 'ebay_user_token_'.$user->id
);
// Set on client
$client = app(\Sashalenz\EbayApi\Client\EbayClient::class);
$client->setUserToken($userToken);