Krave
How to Setup Your AI API

Anthropic API

Before getting started, make sure you have completed the Next.js installation.

Installation

npm install @anthropic-ai/sdk

Project Structure

src/
├── app/
│   ├── api/
│   │   └── anthropic/
│   │       └── route.ts
│   ├── favicon.ico
│   ├── globals.css
│   ├── layout.tsx
│   └── page.tsx

Get your API key here:

aistudio.google.com

Usage

.env
ANTHROPIC_API_KEY=YOUR_ANTHROPIC_API_KEY
src/app/api/gemini/route.ts
import Anthropic from "@anthropic-ai/sdk";
import { NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
    const apiKey = process.env.ANTHROPIC_API_KEY;
    const { prompt } = await req.json();

    if (!apiKey) {
        return NextResponse.json({ error: "API Key is missing." }, { status: 400 });
    }

    if (!prompt) {
        return NextResponse.json({ error: "Prompt is required." }, { status: 400 });
    }

    try {
        const ai = new Anthropic({ apiKey });
        const response = await ai.messages.create({
            model: "claude-sonnet-4-5",
            max_tokens: 1000,
            messages: [
                {
                    role: "user",
                    content: prompt
                }
            ]
        });
            
        return NextResponse.json({ text: response })
    } catch (error: any) {
        console.error("Error generating content:", error);
        return NextResponse.json(
            { error: error.message || "Something went wrong" },
            { status: 400 }
        );
    }
}

Testing

Start the development server:

Terminal
npm run dev

Using Postman

  1. Create a new POST request to http://localhost:3000/api/anthropic
  2. In the Body tab, select raw and add:
{
    "prompt": "What is API?"
}
  1. Click Send

Expected Response

{
    "text": "Generated response from Gemini"
}

On this page