Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Zee Yass
Zee Yass

Posted on

     

Newsletter subscription with Remix and Convertkit.

Used by persons and businesses, newsletter is now very important to any email marketing strategy, it allow you to share important and valuable content with your network of customers.

in this tutorial we are going to setup a newsletter subscription with Remix and Conevrtkit, let's do that.

we are going to need: 👇

  1. convertkit API key.
  2. convertkit Form Id.
  3. fresh installation of remix.

Get convertkit API key and Form Id.
go to account>settings>advanced.

API Key
chose any created form and get the id.

Form ID
in your _.env_

CONVERTKIT_API="https://api.convertkit.com/v3"CONVERTKIT_API_KEY="your_api_key"CONVERTKIT_FORM_ID="your_form_id"
Enter fullscreen modeExit fullscreen mode

make a route newsletter.tsx and create a form inside.
i use tailwindcss for the styling.

import{Form}from"@remix-run/react";exportdefaultfunctionNewsletter(){return(<divclassName="flex items-center justify-center h-screen bg-gray-100"><divclassName="flex flex-col items-center space-y-4"><h1className="font-bold text-2xl text-gray-700 w-4/6 text-center">NewsLetter.</h1><pclassName="text-sm text-gray-500 text-center w-5/6">JoinourNewsletterandgetweeklynews.</p>{/* Newsletter form*/}<Formmethod="post"><inputname="email"type="text"placeholder="type your mail"className="border-2 rounded-lg w-full h-12 px-4"/><buttontype="submit"className="bg-red-400 text-white rounded-md hover:bg-red-500 font-semibold px-4 py-3 w-full">Subscribe</button></Form></div></div>);}
Enter fullscreen modeExit fullscreen mode

Newsletter Form
add remix action and get the submitted value.

...import{ActionFunction}from"@remix-run/node";exportconstaction:ActionFunction=async({request})=>{constformData=awaitrequest.formData();constemail=formData.get("email");console.log(email);...}...
Enter fullscreen modeExit fullscreen mode

after getting the submitted email we can now make a post request to convertkit api and subscribe our user.

...exportconstaction:ActionFunction=async({request})=>{...constAPI_URL=process.env.CONVERTKIT_API;constAPI_KEY=process.env.CONVERTKIT_API_KEY;constFORM_ID=process.env.CONVERTKIT_FORM_ID;constres=awaitfetch(`${API_URL}/forms/${FORM_ID}/subscribe`,{method:"post",body:JSON.stringify({email,api_key:API_KEY}),headers:{"Content-Type":"application/json; charset=utf-8",},});returnawaitres.json();}
Enter fullscreen modeExit fullscreen mode

now we can handle the subscription state, and update ui accordingly, using useActionData and useTransition hooks.

...exportdefaultfunctionNewsletter(){// add remix hooks to handle state.constactionData=useActionData();consttransition=useTransition();letsubmitting=transition.submission;return(<divclassName="flex items-center justify-center h-screen bg-gray-100"><divclassName="flex flex-col items-center space-y-4"><h1className="font-bold text-2xl text-gray-700 w-4/6 text-center">NewsLetter.</h1><pclassName="text-sm text-gray-500 text-center w-5/6">JoinourNewsletterandgetweeklynews.</p>{/* Newsletter form*/}<Formmethod="post"><inputname="email"type="text"placeholder="type your mail"className="border-2 rounded-lg w-full h-12 px-4"/>{/* If subscription succeeded*/}{actionData?.subscription&&<pclassName="w-full text-center text-sm text-green-500">Pleasecheckyouremail,andconfirmsubscription.</p>}{/* If subscription failed*/}{actionData?.error&&<pclassName="w-full text-center text-sm text-red-500">{actionData.message}</p>}{/* update button status when submitting*/}<buttontype="submit"className="bg-red-400 text-white rounded-md hover:bg-red-500 font-semibold px-4 py-3 w-full mt-2">{submitting?"Subscribing ...":"Subscribe"}</button></Form></div></div>);
Enter fullscreen modeExit fullscreen mode

Image description

subscription failed
this is a basic implementation of newsletter subscription, you can optimize further the ui, using the power of remix and react hooks.

et voila, hope this help you creating your newsletter with remix and convertkit, to learn more, i'm sharing my #buildinpublic journey ontwitter.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

bootstrapping saas ideas, build in public.
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp