Newsletter REST API

Newsletter provides an extensive set of functions to interface your custom code with the plugin core features. The API can be accessed programmatically as static methods of the class TNP or via REST calls. REST API are available installing the free Newsletter API extension while the TNP class is always available in the plugin core. You can find the REST API extension on Newsletter extension panel.

General usage notes

Most API function requires a single parameter, an associative array with required and optional values. For example to create a new subscription you’ll write:

TNP::subscribe(['email'=>'email@address.com']);

If you need to pass on the name:

TNP::subscribe(['email'=>'email@address.com', 'name'=>'The name']);

When a call generates an error, in most cases a WP_Error object is returned with an error code and explanation. The returned values of a function should always checked in this way:

$result = TNP::something(...);
if (is_wp_error($result)) {
  // manage the error
}

When using the REST API, the parameters are regular HTTP POST key-value pairs or as a JSON object in the request raw body.

REST API calls require a key (with exceptions) which you can set on API configuration panel.

REST API responses

REST API always respond with HTTP 200 on success. The response can include data as specified for each calls. If something went wrong, you’ll get back a 4xx error. They are:

404 Object not found
403 Not allowed (when the API key is missing or wrong)
400 Bad request, when the parameters are not correct or required parameters are missing

Other than 4xx HTTP status codes, errors are explained in more details decoding the JSON in the response body.

Subscription process

Subscription related functions are used to start a subscription process. That means not only the subscriber’s data is added to the database, but the full subscription process starts with activation and/or welcome emails, notification and so on. If you need to manage the subscriber database, you can use the subscriber management functions (see below).

Subscribe

Starts the subscription process. If the passed email already exists the process can take different paths discussed below.

When called as REST API it can be invoked without the secret key: in this case more strict contraints are applied to the parameters (like for subscriptions coming from a public subscription form). For example if the plugin is configured to make the first name “required” calling the API without a key and without the first name will result in a error. More, for calls without an API key, private lists possibly specified will be ignored or will throw an error.

required default description type
email x a valid email address string
name subscriber name string
surname subscriber surname string
gender subscriber gender string
lists
(since version 5.5.0)
user lists array
profile additional profile fields
(array)
array
send_emails false true or false boolean

HTTP Request

POST /wp-json/newsletter/v1/subscribe

$ curl -X POST http://<your-site>/wp-json/newsletter/v1/subscribe -d '{"email":"test@example.com", "name":"My name", "gender":"f"}'

Function

TNP::subscribe()

TNP::subscribe(['email'=>'test@example.com', 'name'=>'My name', 'gender'=>'f']);

Lists parameter

You have to send an array of numbers with the list ids to be assigned to the subscriber. Pre-assigned lists will be added as well.

Unsubscribe

Unsubscribe the passed email address.

required default description
email x a valid email address
api_key x the string set on the API configuration panel

HTTP Request

POST /wp-json/newsletter/v1/unsubscribe

$ curl -X POST -H "Content-Type: application/json" http://<your-site>/wp-json/newsletter/v1/unsubscribe -d '{"email":"test@example.com", "api_key":"..."}'

Function

TNP::unsubscribe()

TNP::unsubscribe(['email'=>'test@example.com']);

Subscribers management

This set of functions manipulates directly the subscribers database. Subscribers have an internal numeric ID which can be used to identify them and which is stable over time. But even the email address identifies uniquely a subscriber, so it can be used when updating the subscriber data. Usually, when interaction with external system is in place, the email address is used.

Add a subscriber

required default description
email x a valid email address
api_key x the string set on the API configuration panel
name subscriber name
surname subscriber surname
gender subscriber gender
profile additional profile fields
status C subscription status S = Not Confirmed C = Confirmed

HTTP Request

POST /wp-json/newsletter/v1/subscribers/add

$ curl -X POST -H "Content-Type: application/json" http://<your-site>/wp-json/newsletter/v1/subscribers/add -d '{"email":"test@example.com", "api_key":"..."}'

Function

TNP::add_subscriber()

TNP::add_subscriber(['email'=>'test@example.com']);

Updating a subscriber (to be released)

If the updating subscriber is identified by its email, to set a new email the special parameter new_email must be used.

TNP::update_subscriber()

Delete a subscriber

required default description
email x a subscriber email address
api_key x the string set on the API configuration panel

HTTP Request

POST /wp-json/newsletter/v1/subscribers/delete

$ curl -X POST -H "Content-Type: application/json" http://<your-site>/wp-json/newsletter/v1/subscribers/delete -d '{"email":"test@example.com", "api_key":"..."}'

Function

TNP::add_subscriber()

TNP::add_subscriber(['email'=>'test@example.com']);

Subscribers list (last 20)

required default description
api_key x the string set on the API configuration panel

HTTP Request

POST /wp-json/newsletter/v1/subscribers

$ curl -X POST -H "Content-Type: application/json" http://<your-site>/wp-json/newsletter/v1/subscribers -d '{"api_key":"..."}'

Function

TNP::subscribers()

 

Newsletters management

Newsletters list (last 10)

required default description

HTTP Request

GET /wp-json/newsletter/v1/newsletters

$ curl http://<your-site>/wp-json/newsletter/v1/newsletters

Function

TNP::newsletters()