Skip to content

Instantly share code, notes, and snippets.

@birinder-lobana
Last active April 19, 2022 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save birinder-lobana/42786606dda1272b6d13eb2898a272aa to your computer and use it in GitHub Desktop.
Save birinder-lobana/42786606dda1272b6d13eb2898a272aa to your computer and use it in GitHub Desktop.
require('dotenv').config()
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const { Openscreen } = require('@openscreen/sdk')
// initialize Openscreen SDK
const os = new Openscreen().config({
key: process.env.OS_API_KEY,
secret: process.env.OS_API_SECRET,
})
// initialize express server
const app = express()
app.use(bodyParser.json())
app.use(express.static(__dirname + '/public'))
// home page (eg. example.com)
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'))
})
// Openscreen & Klaviyo API endpoint
app.post('/contact', async (req, res) => {
// get request body
const { firstName, lastName, emailAddress, cellPhone, consent } = req.body
// create contact in Openscreen
await os.project(process.env.OS_PROJECT_ID).contacts().create({
firstName,
lastName,
emailAddress,
cellPhone,
consent,
})
// add profile to list in Klaviyo
await axios.post(
`https://a.klaviyo.com/api/v2/list/${process.env.KLAVIYO_LIST_ID}/subscribe?api_key=${process.env.KLAVIYO_PRIVATE_KEY}`,
{
profiles: [
{
$first_name: firstName,
$last_name: lastName,
email: emailAddress,
phone_number: cellPhone,
sms_consent: consent,
},
],
}
)
// success!
res.status(200).end()
})
// start server
app.listen(3000, () => {
console.log('Listening on port 3000')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment