REST Email Marketing API Documentation

Example 2 — Add/update a recipient

Let's assume we're creating a preferences center. The user has entered their email address so we can process the add/update operation. We could fetch the list of topics and build the page dynamically, but normally we know the topics we offer and will want to add specific information about each topic, so we won't build that dynamically in this example.


For this example, we'll assume the person has given us their first and last name via a web form, and that we've collected the topics the person wants to subscribe to.

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;

private static async Task RestExample2(HttpClient client)
{
	string firstName = "Fred";
	string lastName = "Jones";
	var body = new
	{
		accountName = "acme",
		login = "ApiUser",
		password = "YOUR_PASSWORD",
		topics = new[] { "Weekly newsletter" },
		emailAddress = "joe@example.com",
		demographics = new Dictionary<string, string>
		{
			["first Name"] = firstName,
			["last Name"] = lastName
		}
	};

	using (HttpResponseMessage response =
		await client.PostAsJsonAsync("https://example.com/API/Rest/Recipients/AddOne", body))
	{
		response.EnsureSuccessStatusCode();
		string result = await response.Content.ReadAsStringAsync();

		if (result == "Recipient added")
			Console.WriteLine("The recipient has been successfully added.");
		else if (result == "Recipient updated")
			Console.WriteLine("The existing recipient has been updated.");
		else
			Console.WriteLine("Recipient add failed: " + result);
	}
}
			
# encoding: utf-8
require 'rest-client'
require 'json'

lastName = "Jones"
firstName = "Fred"

url = "https://example.com/api/rest/Recipients/AddOne"
args = {'accountName':'acme','login':'ApiUser','password':'YOUR_PASSWORD',
            'topics':['Weekly Newsletter'], 'emailAddress':'joe@example.com',
            'demographics':{'first Name':firstName, 'last Name':lastName}}
resp = RestClient.post url, args.to_json, {"Content-Type": "application/json"}
if resp.code != 200
    puts "Recipient add failed!"
    puts resp.code
    exit
elsif resp.body == "Recipient added"
    puts "The recipient has been successfully added."
elsif resp.body == "Recipient updated"
    puts "The recipient was already in the system but has been updated."
elsif resp.body == "Invalid email address"
    #The recipient email address is not valid. Either syntactically invalid or no DNS MX record for the domain. Deal with the error.
    exit
elsif resp.body == "Email address is banned"
    puts "The email address or domain has been banned in the system. Deal with the error."
    exit
else
    puts "Some other error occured."
    print resp.body
    exit
end

#At this point the recipient is valid, either because they were just created, or because
#they were already in the system, and they are marked as subscribed to our topic.

import requests
import json

lastName = "Jones"
firstName = "Fred"
topicBusinessNews = True

headers = {'Content-Type': 'application/json'}
url = "https://example.com/api/rest/Recipients/AddOne"
args = {'accountName':'acme','login':'ApiUser','password':'YOUR_PASSWORD',
            'topics':['Business News'], 'emailAddress':'joe@example.com',
            'demographics':{'first Name':firstName, 'last Name':lastName}}
resp = requests.post(url, json=args, headers=headers)
if resp.status_code != 200:
    print("Recipient add failed!")
    print(resp.status_code)
    raise SystemExit
elif resp.text == "Recipient added":
    print("The recipient has been successfully added.")
elif resp.text == "Recipient updated":
    print("The existing recipient has been updated.")
elif resp.text == "Invalid email address":
    #The recipient email address is not valid. Either syntactically invalid or no DNS MX record for the domain. Deal with the error.
    raise SystemExit
elif resp.text == "Email address is banned":
    #The email address or domain has been banned in the system. Deal with the error.
    raise SystemExit
else:
    #some other error occured. Check for the conditions and deal with it.
    raise SystemExit

#At this point the recipient is valid, either because they were just created, or because
#they were already in the system, and they are marked as subscribed to our topic.