Skip to content

Latest commit

 

History

History
140 lines (103 loc) · 3.89 KB

File metadata and controls

140 lines (103 loc) · 3.89 KB

Add Contacts

This guide will walk you through the process of adding new contacts to your account using the Python library.

Required Access Level: ModifyContacts

What's contact?

When using Elastic Email, you send emails to contacts – recipients who receive your emails. Contacts can be grouped by created segments or lists.

Preparation

Install Python 3.

Install ElasticEmail library.

Eg. run in terminal pip install ElasticEmail to install from PyPi repository.

Create a new Python file snippet.py and open it in editor of your preference eg. PyCharm (https://www.jetbrains.com/pycharm/download/)

Let's dig into the code

Put the below code to your file.

Load libraries using below code:

import ElasticEmail
from ElasticEmail.models.contact_status import ContactStatus
from ElasticEmail.models.contact_payload import ContactPayload
from pprint import pprint

Generate and use your API key (remember to check a required access level).

Defining the host is optional and defaults to https://api.elasticemail.com/v4

configuration = ElasticEmail.Configuration()
configuration.api_key['apikey'] = 'YOUR_API_KEY'

Pass configuration to an api client and make it instance available under api_client name:

with ElasticEmail.ApiClient(configuration) as api_client:

Create an instance of ContactsApi that will be used to add contacts.

    api_instance = ElasticEmail.ContactsApi(api_client)

Create an array with new contacts.

You can pass an array with up to 1000 contacts.

The Email field is mandatory, the rest is optional.

Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/contactsPost

    contact_payload = [
        ContactPayload(
            Email="johnsmith@domain.com",
            Status=ContactStatus("Active"),
            FirstName="John",
            LastName="Smith",
        ),
    ]

Specify an existing list name in options, otherwise contacts will be added to all contacts.

    list_names = [
        "New list",
    ]

Use try & except block to call contacts_post method from the API to add contacts:

    try:
        # Add Contact
        api_response = api_instance.contacts_post(contact_payload=contact_payload, listnames=list_names)
        pprint(api_response)
    except ElasticEmail.ApiException as e:
        print("Exception when calling ContactsApi->contacts_post: %s\n" % e)

The whole code to copy and paste:

import ElasticEmail
from ElasticEmail.models.contact_status import ContactStatus
from ElasticEmail.models.contact_payload import ContactPayload
from pprint import pprint

# Defining the host is optional and defaults to https://api.elasticemail.com/v4
configuration = ElasticEmail.Configuration()

# Configure API key authorization: apikey
configuration.api_key['apikey'] = 'YOUR_API_KEY'

"""
Add contacts
Example api call that adds new contacts.
Pass array with contact details to add up to 1000 contacts.
Specify a list name in options or add to all contacts.
"""
with ElasticEmail.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = ElasticEmail.ContactsApi(api_client)
    contact_payload = [
        ContactPayload(
            Email="johnsmith@domain.com",
            Status=ContactStatus("Active"),
            FirstName="John",
            LastName="Smith",
        ),
    ]  # [ContactPayload]

    list_names = [
        "New list",
    ]  # [str] | Names of lists to which the uploaded contacts should be added to (optional)

    try:
        # Add Contact
        api_response = api_instance.contacts_post(contact_payload=contact_payload, listnames=list_names)
        pprint(api_response)
    except ElasticEmail.ApiException as e:
        print("Exception when calling ContactsApi->contacts_post: %s\n" % e)

Run the code

python3 snippet.py