Last active
December 10, 2021 22:53
-
-
Save lobot/8e6254dfe0552495ab46d35f504b1c35 to your computer and use it in GitHub Desktop.
Presenting Headless CMS Content as Print Mail - lob-and-contentful
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
app.use((req, res, next) => { | |
next(); | |
}); | |
app.use('/', (req, res, next) => { | |
const contentful = require("contentful"); | |
const client = contentful.createClient({ | |
space: "--CONTENTFUL_SPACE--", | |
accessToken: "--CONTENTFUL_TOKEN--" | |
}); | |
client | |
.getEntry("--POSTCARD_ENTRYID--") | |
.then(entry => createPostcard(entry)) | |
.catch(err => console.log(err)); | |
function createPostcard(entry) { | |
const Lob = require('lob')('--LOB_TEST_API_KEY--'); | |
Lob.postcards.create({ | |
description: 'Contentful Postcard job', | |
to: { | |
name: 'John Doe', | |
address_line1: '101 Green St', | |
address_city: 'San Francisco', | |
address_state: 'CA', | |
address_zip: '94107' | |
}, | |
from: { | |
name: 'Harry Zhang', | |
address_line1: '210 King St', | |
address_line2: '# 6100', | |
address_city: 'San Francisco', | |
address_state: 'CA', | |
address_zip: '94107' | |
}, | |
front: "tmpl_7b983e9340a6c0b", | |
back: "tmpl_17ff0cd48390c87", | |
merge_variables: | |
{ | |
"heading" : entry.fields.heading, | |
"subheading" : entry.fields.subheading, | |
"logo": "http:" + entry.fields.logo.fields.file.url, | |
"main_photo" : "http:" + entry.fields.mainPhoto.fields.file.url, | |
"font" : entry.fields.font[0], | |
"quote" : entry.fields.quote, | |
"agent_name" : entry.fields.agentName, | |
"agent_phone" : entry.fields.agentPhone, | |
"agent_email" : entry.fields.agentEmail, | |
"agent_portrait" : "http:" + entry.fields.agentPortrait.fields.file.url | |
} | |
}) | |
.then((res) => { | |
displayLink(res); | |
}) | |
.catch((e) => { | |
console.log(e); | |
}); | |
}; | |
function displayLink(data) { | |
res.send('Postcard created with data from Contentful. <a href="' + data.url + ' " target="_blank">Preview PDF </a>'); | |
} | |
}); | |
app.listen(3000, () => console.log('Lob and Contentful app is listening on port 3000.')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment