How to test API requests in Mailchimp

We test API requests in Mailchimp in advance to successfully integrate the platform with the site.





API requests are needed in email marketing in order to transfer data from the site to the newsletter platform. This can be transferring subscribers to the list, adding tags to them, changing the values ​​of additional fields, sending trigger letters from the platform upon request.





In sophisticated email marketing platforms, it's all about these kinds of queries. But even in small projects, sometimes there are not enough basic integrations that the platform offers, and you need to make custom API requests.





This article will be useful for email marketers, helping them figure out how to build API requests in Mailchimp and pass them to the backend developer setup. On the site, the request is configured in PHP - we will also give an example of such requests, but we will analyze in more detail the requests that the marketer will use.





In theory, email-marketing should read the FAQ for API from Mailchimp and on its basis to build queries, and to prepare terms of reference for inquiries. In practice, this can be time-consuming and labor-intensive.





I will analyze examples of API requests in Mailchimp that can be tested. You can take examples of these queries, change the data for your project and test.





Preparing for testing

Download Postman  , a program for testing API requests. In it, we will compose a request and send it to the platform. So let's check if our request is correct and if all the data comes to the platform.





Next, for each example, we will compose the request URL, the request body and test the work in Postman. And for some - let's add an example request for PHP, which may be required by the developer.





This is what the program interface consists of.





Request URL

Here we write the request URL and method. There are two most commonly used request methods:





  1. POST if you need to transfer data.





  2. GET , if you need to retrieve data. For example, if we collect a report by email channel in Power BI, then we will use GET requests.





Examples of what we enter in the URL field will be below.





Authorization tab

. , API.





, ExpertSender : . API Toggl, Toggl-. Mailchimp Basic Auth, API key. , Username , Password β€” API key.





, , API Mailchimp , . β€” .





API key   Account β†’ Extras β†’ API keys:





API key API Mailchimp , .





. , . , , , , . JSON XML.





: , , . , , .





Response

Postman  .





   - ,     .     201 created,    API .





   : , ,

    .





URL

https://<dc>.api.Mailchimp.com/3.0/lists/{list_id}/members/

      
      



:





  • {list_id} β€” id ,   ,     ;





  • dc β€”  Mailchimp:   .





  us17.





{
  "email_address": "youremail@gmail.com",
  "status": "subscribed",
  "merge_fields": {
  "FIRSTNAME": "",
  "LASTNAME": ""
  }
}

      
      



:





  • email_address β€” , ;





  • status β€”    : subscribed;





  • merge_fields β€” , .





.   :





  • subscribed β€”   ;





  • pending β€” Double Opt-In ( )  ;





  • unsubscribed β€”  ;





  • cleaned β€”   - .





    ,    .





  :  .       ,    . ,   ,    Mailchimp,    .   Merge Fields   .





  .   MERGE ,   Field label and type.  :





,   , .      . ,     . ,    ,  Postman  .     β€” .





  : .





PHP

backend ,  .





require_once('/path/to/MailchimpMarketing/vendor/autoload.php');

$Mailchimp = new \MailchimpMarketing\ApiClient();
$Mailchimp->setConfig([
    'apiKey' => 'YOUR_API_KEY',
    'server' => 'YOUR_SERVER_PREFIX'
]);
$email = "urist.mcvankab@example.com";
$list_id = "YOUR_LIST_ID";
try {
    $response = $client->lists->addListMember($list_id, [
        "email_address" => "prudence.mcvankab@example.com",
        "status" => "subscribed",
        "merge_fields" => [
          "FNAME" => "Prudence",
          "LNAME" => "McVankab"
        ]
    ]);
    $response = $Mailchimp->lists->addListMember($list_id, $contact);
    // TODO: Get the new member's ID from the response and echo it
    //echo "Successfully added contact as an audience member. The contact's id is {$response->getId()}.";
} catch (MailchimpMarketing\ApiException $e) {
    echo $e->getMessage();
}

      
      



 

URL

https://<dc>.api.Mailchimp.com/3.0/automations/<workflow id>/emails/<email id>/queue

      
      



  :





  • workflow id,





  • email id.





  .     GET . POST -    , ,  GET .  url:





workflow id:





https://<dc>.api.Mailchimp.com/3.0/automations/

      
      



  Postman   ,  . API 3.0.      .





id β€”  , . id . :





https://<dc>.api.Mailchimp.com/3.0/automations/<workflow i>/emails

      
      



 , email id.     .





{
  "email_address": "youremail@gmail.com"
}

      
      



email . , ,   .   ,    β€”    .





URL

https:/<dc>.api.Mailchimp.com/3.0/lists/{list_id}/members/<member_id>/tags

      
      



<member_id> β€” ,   ,  md5 hash   md5 Hash Generator. , Mailchimp  β€”  url   .     hash,  url .





{
      "tags": [
        {
            "name": "product",
            "status": "active"
        }
    ]
}

      
      



The request body is very simple. The status should be active , and in the name field we write the tag that we want to assign to the user. All other information, including the user's email, is passed to the request url.





PHP request

require_once('/path/to/MailchimpMarketing/vendor/autoload.php');

$Mailchimp = new MailchimpMarketing\ApiClient();
$Mailchimp->setConfig([
    'apiKey' => 'YOUR_API_KEY',
    'server' => 'YOUR_SERVER_PREFIX'
]);
$list_id = "YOUR_LIST_ID";
$tag = new MailchimpMarketing\Model\List7();
$tag->setName("MegaInfluencer");
$tag->setStaticSegment(["dolly.parton@example.com", "rihanna@example.com"]);
try {
    $response = $Mailchimp->lists->createSegment($list_id, $tag);
    echo "Tag successfully created! Your tag id is " . $response->getId();
} catch (MailchimpMarketing\ApiException $e) {
    echo $e->getMessage();
}

      
      



We wish you successful integrations!








All Articles