I'm using Sendgrid's new API (v3) with Node js + libraries @sendgrid/mail
and @sendgrid/client
.
What I want to achieve: send a weekly digest to all my contacts (except those who unsubscribe from this group). I use a template I created via Sendgrid thanks to its template_id, as well as dynamic template data to populate the mail with the weekly data.
Problem:
I can send a marketing campaign using /v3/marketing/singlesends to all my contacts with my template but I can't send it with my dynamic template data.
I can send one email at a time with my template + dynamic template data but for this I need to retrieve all my contacts first and I can only retrieve the last 50 contacts from this endpoint /v3/marketing/contacts (they disabled the pagination). I may have been able to retrieve them all from this endpoint /contactdb/recipients, the problem is I created my Sendgrid account after they released their new API so I can't access it.
Any idea on how to perform this?
Current code:
1) Configure campaign
const sgClient = require('@sendgrid/client')
var params={
'name': 'Weekly Digest #'+nb,
'sender_id': sg_sender_id,
'suppression_group_id': sg_unsub_group_id,
'template_id': id_template,
'dynamicTemplateData': template_params,
//also tried with 'dynamic_template_data' instead, not working
'filter': {'send_to_all': true}
}
let url='/v3/marketing/singlesends/' + campaign_id.toString('base64')
const request = {
method: 'PATCH',
url: url,
body: params
}
sgClient.setApiKey(config.sendgrid_key)
sgClient.request(request)
.then(([response, body]) => {
console.log('body:', body)
})
.catch(error => {
console.error('error:', error.toString())
})
2) Send campaign
let url='/v3/marketing/singlesends/' + campaign_id.toString('base64') + '/schedule'
let params={'send_at': 'now'}
const request = {
method: 'PUT',
url: url,
body: params
}
sgClient.setApiKey(config.sendgrid)
sgClient.request(request)
.then(([response, body]) => {
console.log('body:', body)
})
.catch(error => {
console.error('error:', error.toString())
})