Mailchimp Api Integration and Authentication in laravel

Mailchimp Api Integration Example; In this tutorial you will learn how to integrate mailchimp in laravel application. Now a days more then projects implement other apis in a single app.

We have implemented the mailchimp api integration and authentication in laravel php with step by step guide. We have used the guzzle api for get and post request for hitting api.

Step 1: Create app in Mailchimp

First go to this link and register to mailchimp. After register successfully verify your account go to your email and varify the mailchimp account. After verify the account login your account then get the client id and client secret.

Step 2: Get Client Id and Client Secrete

Here we show you how to create app and how to get the client Id and client secret from mailchimp account.

Login accound and direct hit and then select Extras then click Registered Apps then create an app. Add the app name, description and add callback URI here.

We have added the image here you can check them. After adding all your details now save the credentials. After saving the form you will see client Id and Client Secret just like below image.

Step 3: Update Client Id and Secret in .env File

After successfully create app in mailchimp and getting the client id and secret, now update these in .env file.


Step 4: In Blade File

Now add a button in blade file. You can update just like below.

<a href="route('redirect')" class="btn btn-primary">

Step 5: Make routes

Now add two routes in web.php, one is for redirect to mailchimp and another is getting the access token.

Route::get('/redirect', 'MailchimpController@redirect')->name('redirect');
Route::get('/callback', 'MailchimpController@callback')->name('callback');

Step 6: Create Controller

Create a new controller just run the below command.

php artisan make:controller MailchimpController

After successfully created the controller, next open the app\Http\Controllers\MailchimpController.php file and update the below code on it.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use GuzzleHttp\Client;

class MailchimpController extends Controller
  public function __construct()
    $this->clientId = env('MAILCHIMP_CLIENT_ID');
    $this->clientSecret = env('MAILCHIMP_CLIENT_SECRET');
    $this->redirectUri = route('callback');
    $this->client = new Client;
    $this->loginRoute = '';

  public function redirect(Request $request)
      $url = $this->loginRoute.'/authorize?response_type=code&client_id='.$this->clientId.'&redirect_uri='.$this->redirectUri;

      return redirect()->to($url);

  public function callback(Request $request)
      try {
          $response = $this->client->post($this->loginRoute.'/token', [
            'form_params' => [
              'grant_type' => 'authorization_code',
              'code' => $request->code,
              'client_id' => $this->clientId,
              'client_secret' => $this->clientSecret,
              'redirect_uri' => $this->redirectUri,

        $response = json_decode($response->getBody()->getContents(), true);

      } catch (Exception $e) {
      return redirect()->route('home')->with('danger', 'Auth token has expired');

     return redirect()->route('home')->with('success', 'Mailchimp connection successfully connected!');

In above callback function we have \Log::info the response you can check this response in your logs files, where you can see the access token. Thats over, I hope you enjoyed more with Mailchimp api integration and authentication in laravel app. If you have any queries or questions let me know in comment section.

Leave a Comment