Table of Contents

Connecting with an API key

Unlike most Cloud Pack components, TTMSFNCCloudMailChimpMarketing does not use the OAuth consent flow. Mailchimp authenticates with a single API key that you generate in your Mailchimp account, so connecting is as simple as assigning the key and calling an operation. This chapter covers setting the key, how the data center is selected, and where request failures are reported.

Setting the API key

Assign your key to APIKey before calling any operation. The key ends in a -usXX suffix that identifies your Mailchimp data center, so the component derives the server from the key itself — there is no separate URL or Connect step. The first operation you call (typically GetLists) both authenticates and returns data.

procedure TForm1.ConnectMailChimp(const AKey: string);
begin
  FMailChimp := TTMSFNCCloudMailChimpMarketing.Create(Self);

  // The API key from your Mailchimp account. Its trailing -usXX suffix selects
  // the data center, so no separate server URL is needed.
  FMailChimp.APIKey := AKey;

  FMailChimp.OnError := MailChimpError;
  FMailChimp.OnGetLists := MailChimpGetLists;

  // No OAuth/Connect step: just call an operation. Result arrives in OnGetLists.
  FMailChimp.GetLists(100, False);
end;

procedure TForm1.MailChimpError(Sender: TObject; AErrorMessage: string);
begin
  // Every failed request reports here, separate from the success events.
  ShowMessage('Mailchimp error: ' + AErrorMessage);
end;

Error reporting

Mailchimp operations are asynchronous and each success has its own event (such as OnGetLists or OnAddCampaign). Failures are reported separately through a single OnError event that carries the message text — wire it once so no failed request goes unnoticed. The success events therefore do not carry a request-result parameter; treat OnError as the catch-all for problems.

Pitfalls

  • Set APIKey before the first call. Operations made without a key fail and surface through OnError.
  • Use a key with the right account scope. A key from a different account or with insufficient access authenticates but rejects the operation.
  • Handle OnError. Because success events have no result flag, OnError is the only place failures appear.

See also