Skip to content

Instantly share code, notes, and snippets.

@hypervillain
Last active October 5, 2018 16:28
Show Gist options
  • Save hypervillain/c1fa0c663b47381d5fa25caf7f4024dc to your computer and use it in GitHub Desktop.
Save hypervillain/c1fa0c663b47381d5fa25caf7f4024dc to your computer and use it in GitHub Desktop.
Add Subscriber to Revue (https://getrevue.co) using Serverless (AWS Lambda)
'use strict';
/*
Before creating your service,
add your API key to serverless.yml file
For example, a valid conf:
service:
name: revue
provider:
name: aws
runtime: nodejs6.10
profile: default
environment:
api_key: XXX_YOUR_API_KEY
functions:
addSubscriber:
handler: addSubscriber.handler
events:
- http: POST addSubscriber
cors: true
*/
const request = require('request');
const url = 'https://www.getrevue.co/api/v2/subscribers';
module.exports.handler = (event, context, callback) => {
const email = event.email;
if (!process.env.api_key) {
return callback(null, {
statusCode: 400,
body: JSON.stringify({
message: `Please provide your Revue API key (https://www.getrevue.co/app/integrations)`,
errorType: 'NO_API_KEY_PROVIDED',
}),
});
}
if (!email) {
return callback(null, {
statusCode: 400,
body: JSON.stringify({
message: `Please provide us with a valid email!`,
errorType: 'NO_EMAIL_PROVIDED',
}),
});
}
return request.post({
url,
headers: {
Authorization: `Token token="${process.env.api_key}"`,
},
formData: {
email,
// if you don't want double opt in
// set `double_opt_in` option to false
}
}, (err, _, body) => {
if (err) {
return callback(null, {
statusCode: 500,
body: JSON.stringify({
message: `An unexpected (and rare) error has occured. Please contact us`,
errorType: 'UNEXPECTED',
}),
});
}
if (body && body.error && body.error.email) {
return callback(null, {
statusCode: 400,
body: JSON.stringify({
message: `${email} is already a subscriber`,
errorType: 'EMAIL_EXISTS',
}),
});
}
return callback(null, {
statusCode: 200,
body: JSON.stringify({
message: 'You\'re all set! Please check your emails for our confirmation link!',
}),
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment