Create post and send via email using Admin API

I’m trying to use the admin api and Javascript client to load an HTML post and send it via email. I can add a post, but the API documentation is unclear about how to schedule it to be send as email in the Javascript client. Here is the relevant text in the docs:

To send a post by email, the newsletter query parameter must be passed when publishing or scheduling the post, containing the newsletter’s slug.

Does this mean that I cannot do this from within the client and must use the endpoint? It’s not clear.

In cases like this, it’s time “use the source, Luke!”.

The source for the Admin API shows that the edit() method has an undocumented second parameter that accepts query parameters:

So that’s where you can adding the query params for newsletter mailings described here:

@mjw Where can documentation patches for the JavaScript Admin API be submitted to document the second parameter for “edit()” calls? I looked in the SDK repo, but there’s no mention there of where the docs are stored. Then I found the “docs” repo and saw that it is archived, but there’s mention in that archived README of where to update the docs instead.

@markstos Thanks for doing the research :smile: The docs themselves are in a private repo, but I think you could add this as an issue in the SDK. It’s also on my radar now, so I’ll work on updating the docs to reflect this option!

If something’s missing from the docs in the future, you can message me on the Forum, too!

1 Like

Thanks for this, and being able to send the query params this way would help. Syntactically, how would I do this? I’m using requests in Python. Thanks for any guidance.

If you are unsure how to add a query string to a request in Python, I suggest trying the official JavaScript SDK instead.

Thanks. I have some JS code that successfully posts via the API , makes status published, and includes the query string params. But it won’t send out via email on the Admin API, despite the query string being included (I think).

Does this make sense to you? And thanks again.

var html = '<p>Testing email sending through API</p>'

api.posts.add({
    title: 'Testing Email Sending Through API',
    html: html,
    status: 'published',
    }, {
    source: 'html',
    newsletter: 'default-newsletter',
    }
    );

I have tried to modify and email existing posts, as well as send new posts via email, and neither version of the code sends the email. I can modify properties, but not send anything via email. I can obviously send via email manually.

Update

Okay, I got it to work. I had to post the entry first, and then modify its status to published with the edit function, and then add the query string params. Thanks for your help.

1 Like