Package: mailjet-rest
Official Mailjet Python WrapperOverviewWelcome to theMailjetofficial Python API wrapper!Check out all the resources and Python code examples in the officialMailjet Documentation.Table of contentsCompatibilityInstallationAuthenticationMake your first callClient / Call configuration specificsAPI versioningBase URLRequest examplesPOST requestSimple POST requestUsing actionsGET requestRetrieve all objectsUse filteringRetrieve a single objectPUT requestDELETE requestContributeCompatibilityThis library officially supports the following Python versions:v2.7v3.5v3.6InstallationUse the below code to install the wrapper:(sudo)pipinstallmailjet_restAuthenticationThe Mailjet Email API uses your API and Secret keys for authentication.Graband save your Mailjet API credentials.exportMJ_APIKEY_PUBLIC='your api key'exportMJ_APIKEY_PRIVATE='your api secret'Initialize yourMailjetclient:# import the mailjet wrapperfrommailjet_restimportClientimportos# Get your environment Mailjet keysAPI_KEY=os.environ['MJ_APIKEY_PUBLIC']API_SECRET=os.environ['MJ_APIKEY_PRIVATE']mailjet=Client(auth=(API_KEY,API_SECRET))Make your first callHere's an example on how to send an email:frommailjet_restimportClientimportosapi_key=os.environ['MJ_APIKEY_PUBLIC']api_secret=os.environ['MJ_APIKEY_PRIVATE']mailjet=Client(auth=(api_key,api_secret),version='v3.1')data={'Messages':[{"From":{"Email":"$SENDER_EMAIL","Name":"Me"},"To":[{"Email":"$RECIPIENT_EMAIL","Name":"You"}],"Subject":"My first Mailjet Email!","TextPart":"Greetings from Mailjet!","HTMLPart":"<h3>Dear passenger 1, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!"}]}result=mailjet.send.create(data=data)printresult.status_codeprintresult.json()Client / Call Configuration SpecificsAPI VersioningThe Mailjet API is spread among three distinct versions:v3- The Email APIv3.1- Email Send API v3.1, which is the latest version of our Send APIv4- SMS API (not supported in Python)Since most Email API endpoints are located underv3, it is set as the default one and does not need to be specified when making your request. For the others you need to specify the version usingversion. For example, if using Send APIv3.1:# import the mailjet wrapperfrommailjet_restimportClientimportos# Get your environment Mailjet keysAPI_KEY=os.environ['MJ_APIKEY_PUBLIC']API_SECRET=os.environ['MJ_APIKEY_PRIVATE']mailjet=Client(auth=(API_KEY,API_SECRET),version='v3.1')For additional information refer to ourAPI Reference.Base URLThe default base domain name for the Mailjet API isapi.mailjet.com. You can modify this base URL by setting a value forapi_urlin your call:mailjet=Client(auth=(api_key,api_secret),api_url="https://api.us.mailjet.com/")If your account has been moved to Mailjet'sUS architecture, the URL value you need to set ishttps://api.us.mailjet.com.Request examplesPOST requestSimple POST request"""Create a new contact:"""frommailjet_restimportClientimportosapi_key=os.environ['MJ_APIKEY_PUBLIC']api_secret=os.environ['MJ_APIKEY_PRIVATE']mailjet=Client(auth=(api_key,api_secret))data={'Email':'Mister@mailjet.com'}result=mailjet.contact.create(data=data)printresult.status_codeprintresult.json()Using actions"""Manage the subscription status of a contact to multiple lists:"""frommailjet_restimportClientimportosapi_key=os.environ['MJ_APIKEY_PUBLIC']api_secret=os.environ['MJ_APIKEY_PRIVATE']mailjet=Client(auth=(api_key,api_secret))id='$ID'data={'ContactsLists':[{"ListID":"$ListID_1","Action":"addnoforce"},{"ListID":"$ListID_2","Action":"addforce"}]}result=mailjet.contact_managecontactslists.create(id=id,data=data)printresult.status_codeprintresult.json()GET RequestRetrieve all objects"""Retrieve all contacts:"""frommailjet_restimportClientimportosapi_key=os.environ['MJ_APIKEY_PUBLIC']api_secret=os.environ['MJ_APIKEY_PRIVATE']mailjet=Client(auth=(api_key,api_secret))result=mailjet.contact.get()printresult.status_codeprintresult.json()Using filtering"""Retrieve all contacts that are not in the campaign exclusion list:"""frommailjet_restimportClientimportosapi_key=os.environ['MJ_APIKEY_PUBLIC']api_secret=os.environ['MJ_APIKEY_PRIVATE']mailjet=Client(auth=(api_key,api_secret))filters={'IsExcludedFromCampaigns':false,}result=mailjet.contact.get(filters=filters)printresult.status_codeprintresult.json()Retrieve a single object"""Retrieve a specific contact ID:"""frommailjet_restimportClientimportosapi_key=os.environ['MJ_APIKEY_PUBLIC']api_secret=os.environ['MJ_APIKEY_PRIVATE']mailjet=Client(auth=(api_key,api_secret))id='Contact_ID'result=mailjet.contact.get(id=id)printresult.status_codeprintresult.json()PUT requestAPUTrequest in the Mailjet API will work as aPATCHrequest - the update will affect only the specified properties. The other properties of an existing resource will neither be modified, nor deleted. It also means that all non-mandatory properties can be omitted from your payload.Here's an example of aPUTrequest:"""Update the contact properties for a contact:"""frommailjet_restimportClientimportosapi_key=os.environ['MJ_APIKEY_PUBLIC']api_secret=os.environ['MJ_APIKEY_PRIVATE']mailjet=Client(auth=(api_key,api_secret))id='$CONTACT_ID'data={'Data':[{"Name":"first_name","value":"John"},{"Name":"last_name","value":"Smith"}]}result=mailjet.contactdata.update(id=id,data=data)printresult.status_codeprintresult.json()DELETE requestUpon a successfulDELETErequest the response will not include a response body, but only a204 No Contentresponse code.Here's an example of aDELETErequest:"""Delete an email template:"""frommailjet_restimportClientimportosapi_key=os.environ['MJ_APIKEY_PUBLIC']api_secret=os.environ['MJ_APIKEY_PRIVATE']mailjet=Client(auth=(api_key,api_secret))id='Template_ID'result=mailjet.template.delete(id=id)printresult.status_codeprintresult.json()ContributeMailjet loves developers. You can be part of this project!This wrapper is a great introduction to the open source world, check out the code!Feel free to ask anything, and contribute:Fork the project.Create a new branch.Implement your feature or bug fix.Add documentation to it.Commit, push, open a pull request and voila.If you have suggestions on how to improve the guides, please submit an issue in ourOfficial API Documentation repo.