Last active
April 7, 2021 06:28
-
-
Save mackenly/0caf71256d2f93db388de88c124b6e80 to your computer and use it in GitHub Desktop.
Ghost to ConvertKit Connector
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
/* | |
Serverless Function to connect Ghost to ConvertKit | |
Required Enviroment Vairables: | |
API_KEY - Your ConvertKit API Key | |
FORM_ID - The ConvertKit form's ID you wish to subscribe users to | |
TAG_ID - Tag to mark users with in ConvertKit | |
Ghost Settings: | |
In Ghost create a new custom integration and add webhooks for new member and | |
updated member. Point the webhooks to your serverless function. (I'm running mine on Cloudflare Workers) | |
Note: To disable the Tag functionality simply comment out line 42. Tags are optional in ConvertKit. | |
Also, to find the FORM_ID and TAG_ID you will need to look at the URL in the ConvertKit dashboard. | |
*/ | |
async function gatherResponse(response) { | |
const { headers } = response | |
const contentType = headers.get("content-type") || "" | |
if (contentType.includes("application/json")) { | |
return JSON.stringify(await response.json()) | |
} | |
else if (contentType.includes("application/text")) { | |
return await response.text() | |
} | |
else if (contentType.includes("text/html")) { | |
return await response.text() | |
} | |
else { | |
return await response.text() | |
} | |
} | |
async function sendUser(user_name, user_email){ | |
const convertkit_url = 'https://api.convertkit.com/v3/forms/' + FORM_ID + '/subscribe' | |
const convertkit_key = API_KEY | |
const convertkit_data = { | |
api_key: convertkit_key, | |
email: user_email, | |
first_name: user_name, | |
tags: [{TAG_ID}] | |
} | |
const init = { | |
body: JSON.stringify(convertkit_data), | |
method: "POST", | |
headers: { | |
"content-type": "application/json;charset=UTF-8", | |
}, | |
} | |
const response = await fetch(convertkit_url, init) | |
const results = await gatherResponse(response) | |
return new Response(results, init) | |
} | |
function simpleResponse(statusCode, message) { | |
let resp = { | |
message: message, | |
status: statusCode | |
} | |
return new Response(JSON.stringify(resp), { | |
headers: new Headers([["Content-Type", "application/json"]]), | |
status: statusCode | |
}) | |
} | |
addEventListener("fetch", event => { | |
event.respondWith(WebhookHandler(event.request)) | |
}) | |
async function WebhookHandler(request) { | |
if (request.method !== "POST") { | |
return simpleResponse( | |
200, | |
`Please send a POST request :)` | |
) | |
} | |
try { | |
const formData = await request.json() | |
const headers = await request.headers | |
const user_email = formData.member.current.email | |
const user_name = formData.member.current.name | |
return await sendUser( | |
user_name, | |
user_email) | |
} catch (e) { | |
return simpleResponse( | |
200, | |
`Error: ${e} ` | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment