Laravel Amazon Ads API


Laravel Amazon Ads API

Laravel 9 integrate Amazon Ads API, with the Amazon Advertising Sdk, a way to interact with amazon campaigns and products

Laravel Amazon Ads API

First it is necessary to install the sdk with composer, we will use the following command in a terminal

composer require rovast/amazon-advertising-api-php-sdk

Once the package is installed, it will be found in the composer.json file in the part of require “rovast/amazon-advertising-api-php-sdk”: “^1.2”

It is necessary to be authenticated to be able to use the api and obtain information about our campaigns, access to this api is quite strict, we will do some tests without being authenticated

We create some environment variables in the .env file

laravel .env


Then in the config/services.php folder we add the following properties

'amazon' => [
       'access' => env('TOKEN_ACCESS'),
       'refresh' => env('TOKEN_REFRESH'),
       'client' => env('CLIENT_SECRET'),
   ],

We will create a controller for testing

php artisan make:controller TestController 

Now we add the application logic


namespace App\Http\Controllers;

use AmazonAdvertisingApi\Client;

class TestController extends Controller
{
	public $client = null;
    
    public function __construct()
    {      

    	$config = $this->getConfig();    	

    	$this->client = new \AmazonAdvertisingApi\Client($config);    	

    	print_r($this->client);
     
    }

    public function getConfig()
    { 
    
    	$client_secret = env('CLIENT_SECRET');

    	$access = env('TOKEN_ACCESS');

    	$refresh = env('TOKEN_REFRESH');

    	return $this->config = array(
    		"clientId" => "amzn1.application-oa2-client.7281e9049e44d9078ge8d6cg1baea0a4",
    		"clientSecret" => $client_secret,
    		"region" => "na",
    		"accessToken" => $access,
    		"refreshToken" => $refresh,
    		"sandbox" => true);

    }
   
    public function index(Request $request)
    {    	

    	$campaigns = $this->client->listCampaigns();  	

    	return response()->json($campaigns); 
    }
} 

The process is divided into three parts, we will set the configuration in the client class, remember to import the namespace, in the getConfig() method we set the values and in the index method we will list all the campaigns, it is necessary to add the routes

use App\Http\Controllers\TestController;
Route::resource('test', TestController::class); 

This is an example with resource type routes, now we will try to obtain a response, which shows us a status code 401 since we are not authenticated with this api

laravel response