About: 👨💻 Full Stack Developer | 🤖 Machine Learning Developer | 🤝 Dev Relations Pro – 💼 Available for Hire | 24k+ Followers | 355k+ Views
Location:
India
Joined:
Apr 5, 2022
🎉 Let's Build an AI Twitter Post Project Using Gemini API, Next.js, and TailwindCSS 🚀
Publish Date: Jan 1
92 19
2025 is here, and what better way to kick off the year than by building an awesome Twitter Post project? 🎯 In this blog, I’ll show you how to integrate the Gemini API with Next.js and style it using TailwindCSS to create a simple but effective post system. Plus, we'll use the Gemini API Key to fetch posts and display them.
Let's firstly preview how our final mini project looks like :
Familiarity with Next.js/React.js and TailwindCSS (if you need a refresher or want to learn Nextjs/Reactjs, check out this course :
1. Create a Next.js Project 🖥️
Start by creating a new Next.js project:
npx create-next-app twitter-post
cd twitter-post
2. Install Gemini API Package 📦
Now, let’s install the Gemini npm package:
npm i @google/generative-ai
Create a .env.local file in the root directory and add your Gemini API key:
GEMINI_API_KEY=your_api_key_here
3. Fetch Twitter Posts with Gemini API 🔥
Create app/api/submit/route.tspath in project,In route.ts we will fetch the Twitter-like posts using the Gemini API and display them.
import{GoogleGenerativeAI}from'@google/generative-ai';import{NextResponse}from'next/server';constAPI_KEY=process.env.GEMINI_AI_API_KEY||"";exportasyncfunctionPOST(req:Request){const{description}=awaitreq.json();if (!description){returnNextResponse.json({error:'Description is required.'},{status:400});}try{constgenAI=newGoogleGenerativeAI(API_KEY);constmodel=awaitgenAI.getGenerativeModel({model:'gemini-1.5-flash'});constprompt=`Generate twitter tweet on the basis of this description: ${description}`;constresult=awaitmodel.generateContent([prompt]);if (result&&result.response){constgeneratedText=awaitresult.response.text();returnNextResponse.json({tweet:generatedText});}else{thrownewError('No response received from model.');}}catch (error){console.error('Error generating tweet:',error);returnNextResponse.json({error:'Failed to generate tweet'},{status:500});}}
Above code's functionality description is:
Generates Tweet: Takes a description, uses Google's AI to create a tweet based on it.
Error Handling: Returns errors if no description is provided or if AI fails.
AI Model Used: Uses gemini-1.5-flash for content generation.
4. Main front-end logic of handling : generate tweet, copy tweet. regenerate tweet is :
This is simple server side html, tailwindcss component :
import{RiTwitterXLine}from"react-icons/ri";importInteractiveFormfrom"./components/InteractiveForm";exportdefaultfunctionHome(){return (<divclassName="flex flex-col justify-center items-center min-h-screen bg-[#1a1a1a] w-full"><RiTwitterXLinesize={50}color="white"/><divclassName="flex flex-col justify-center items-center mt-7 w-full max-w-4xl py-3"><pclassName="text-white text-4xl font-extrabold">Idea to tweet in seconds.</p><pclassName="text-white text-2xl">Tweet Craft is your superhuman tweet-writing expert.</p><InteractiveForm/></div></div>);}
and the main part that handle all the tweet generation, copy,regenrate is client side component which is :
You can easily change colors, spacing, and other design elements using Tailwind classes.
5. Run the Project 🚀
Now, it’s time to run your project:
npm run dev
Open http://localhost:3000 in your browser, and you’ll see your Twitter-like post feed in action! 🎉
Contribute to the Repository 💻
I would love to see what you can build with this project! Feel free to fork this repository, create issues, or even submit pull requests. Let's make this project even more powerful together!
Like embedding twitter api's,adding functionality of suggestion buttons etc.
Tweet Craft is an AI-powered application built with Next.js that generates tweets based on user descriptions. By using Gemini's API, the app enables users to easily craft tweets based on their input, making it perfect for those looking for quick and creative social media content.
Tweet Craft
Tweet Craft is an AI-powered application built with Next.js that generates tweets based on user descriptions. By using Gemini's API, the app enables users to easily craft tweets based on their input, making it perfect for those looking for quick and creative social media content
Video Link : https://vimeo.com/1043260556?share=copy
Features
AI Tweet Generation: Users provide a description, and the app generates a tweet based on the input.
Gemini API Integration: The app utilizes Gemini's API to process natural language descriptions and create tweets.
Installation
Prerequisites
Node.js (>= 18.0)
npm or yarn (package manager)
Steps to Get Started
Clone the repository:
git clone https://github.com/Jagroop2001/tweet-craft
cd tweet-craft
Install the dependencies:
npm install
# OR
yarn install
Set up your Gemini API key:
Create a .env.local file in the root of the project.
Wow, @jagroop2001 this is an amazing project! 🚀 Combining Gemini API, Next.js, and TailwindCSS is such a powerful stack—can't wait to clone this project. Keep up the great work! &
Happy New year !!
Hi @jagroop2001 !!
This project is really inspiring especially for me as I am very new to coding and I read this blog and undertand the functionality and will surely clone this and extend this further.
I am very noob so it may possible that I need your help.
Would you mentor me in this project so that we can extend this further ?
Yes I will completely mentor you whenever you need my help.
You can reach me on linkedin,twitter,email etc. everything is added on my profile and all the best.
This web app generates tweets based on the description provided by the user. It's a handy tool for creating engaging and concise tweets effortlessly.
The project is open-source, and contributors are actively improving it. You can check it out and contribute on GitHub: github.com/Jagroop2001/tweet-craft
Wow, @jagroop2001 this is an amazing project! 🚀 Combining Gemini API, Next.js, and TailwindCSS is such a powerful stack—can't wait to clone this project. Keep up the great work! &
Happy New year !!