Installation

You can install the package from npm:
Minimum Node version required: 18.0.0. You will need a Cocoonmail API key to use the package. In your Cocoonmail account, go to the API Settings page and click Generate key. Copy this key and save it in your application code (for example as Cocoonmail_API_KEY in an .env file).

Next.js guide

Read our guide for sending email from Next.js projects.

Usage

from "cocoonmail";
See the API documentation to learn more about rate limiting and error handling.

Default contact properties

Each contact in Cocoonmail has a set of default properties. These will always be returned in API results.
  • id
  • email
  • firstName
  • lastName
  • source
  • subscribed
  • userGroup
  • userId

Custom contact properties

You can use custom contact properties in API calls. Please make sure to add custom properties in your Cocoonmail account before using them with the SDK.

Methods

testApiKey()

Test that an API key is valid. API Reference

Parameters

None

Example

Response

This method will return a success or error message:

createContact()

Create a new contact. API Reference

Parameters

Examples

Response

This method will return a success or error message:

updateContact()

Update a contact. Note: To update a contact’s email address, the contact requires a userId value. Then you can make a request with their userId and an updated email address. API Reference

Parameters

Example

Response

This method will return a success or error message:

findContact()

Find a contact. API Reference

Parameters

You must use one parameter in the request.

Examples

resp = await cocconmail.findContact({ email: });

Response

This method will return a list containing a single contact object, which will include all default properties and any custom properties. If no contact is found, an empty list will be returned.

deleteContact()

Delete a contact, either by email address or userId. API Reference

Parameters

You must use one parameter in the request.

Example

Response

This method will return a success or error message:

getMailingLists()

Get a list of your account’s mailing lists. Read more about mailing lists API Reference

Parameters

None

Example

Response

This method will return a list of mailing list objects containing id, name and isPublic attributes. If your account has no mailing lists, an empty list will be returned.

sendEvent()

Send an event to trigger an email in Cocoonmail. Read more about events API Reference

Parameters

Examples

Response

This method will return a success or error:

sendTransactionalEmail()

Send a transactional email to a contact. Learn about sending transactional email API Reference

Parameters

Examples

Response

This method will return a success or error message.
If there is a problem with the request, a descriptive error message will be returned:

getCustomFields()

Get a list of your account’s variables. These are custom properties that can be added to contacts to store extra data. Read more about contact properties API Reference

Parameters

None

Examples

Response

This method will return a list of custom field objects containing key, label and type attributes. If your account has no variables, an empty list will be returned.

Version history

  • v3.3.0 (Sep 9, 2024) - Added testApiKey() method.
  • 3.2.0 (Aug 23, 2024) - Added support for a new mailingLists attribute in findContact().
  • v3.1.0 (Aug 12, 2024) - The SDK now accepts null as a value for contact properties in createContact(), updateContact() and sendEvent(), which allows you to reset/empty properties.
  • v3.0.0 (Jul 2, 2024) - [sendTransactionalEmail()]() now accepts an object instead of separate parameters (breaking change).
  • v2.2.0 (Jul 2, 2024) - Deprecated. Added new addToAudience option to sendTransactionalEmail().
  • v2.1.1 (Jun 20, 2024) - Added support for mailing lists in createContact(), updateContact() and sendEvent().
  • v2.1.0 (Jun 19, 2024) - Added support for new List mailing lists endpoint.
  • v2.0.0 (Apr 19, 2024)
    • Added userId as a parameter to findContact(). This includes a breaking change for the findContact() parameters.
    • userId values must now be strings (could have also been numbers previously).
  • v1.0.1 (Apr 1, 2024) - Fixed types for sendEvent().
  • v1.0.0 (Mar 28, 2024) - Fix for ESM types. Switched to named export- .
  • v0.4.0 (Mar 22, 2024) - Support for new eventProperties in sendEvent(). This includes a breaking change for the sendEvent() parameters.
  • v0.3.0 (Feb 22, 2024) - Updated minimum Node version to 18.0.0.
  • v0.2.1 (Feb 6, 2024) - Fix for ESM imports.
  • v0.2.0 (Feb 1, 2024) - CommonJS support.
  • v0.1.5 (Jan 25, 2024) - getCustomFields() now returns type values for each contact property.
  • v0.1.4 (Jan 25, 2024) - Added support for userId in sendEvent() request. Added missing error response type for sendEvent() requests.
  • v0.1.3 (Dec 8, 2023) - Added support for transactional attachments.
  • v0.1.2 (Dec 6, 2023) - Improved transactional error types.
  • v0.1.1 (Nov 1, 2023) - Initial release.