Read through this guide to set up xAI’s Grok API & get familiar with how it is used in the Twitter Brand Personality Generator.

Set up Grok API

First, create an account or sign in at the xAI Console. Next, navigate to the “API Keys” section and click “Create new API key”. Make sure to save your API key somewhere safe and do not share it with anyone.

Once you have your API key, paste it in your .env file:

XAI_API_KEY=your_xai_api_key

Demo App

The Twitter Brand Personality Generator showcases Grok’s capabilities in analyzing brand voice and generating engaging social media content.

Twitter Brand Personality Generator

Generate Twitter brand personalities with tone analysis, example tweets, and risk assessment

Structure

The API route in app/api/(apps)/grok/route.ts follows this structure:

  1. Takes in brand details from the front-end
  2. Generates a prompt based on the brand inputs
  3. Calls Grok API using the prompt + JSON schema
  4. Extracts the structured JSON response containing:
    • Brand tone scores
    • Common phrases and emojis
    • Example tweets with engagement metrics
    • PR risk analysis
  5. Stores the response in Supabase
  6. Returns a uuid to the front-end for fetching & displaying the analysis

More information on the codebase structure can be found here:

Structure

Understand the project structure of the codebase

API Response Structure

The Grok API returns structured JSON containing:

{
  tweetStyle: {
    toneScore: {
      cringe: number;
      corporate: number;
      sassiness: number;
      fellowKids: number;
    };
    emojiUsage: string[];
    commonPhrases: string[];
  };
  seoMetadata: {
    title: string;
    subtitle: string;
    description: string;
  };
  exampleTweets: Array<{
    content: string;
    predictedEngagement: {
      likes: number;
      retweets: number;
      replies: number;
      ratioRisk: number;
    };
  }>;
  contentStrategy: {
    tweetTypes: string[];
    controversyRisk: {
      level: number;
      scenarios: string[];
    };
    engagementTactics: string[];
  };
}

The structured output enables rich visualizations including radar charts for tone analysis, Twitter-style cards for example tweets, and risk assessment indicators.