Tutorials

How to Use Kimi with the Vercel AI SDK — Moonshot Provider Guide

On this page

In this tutorial, you'll learn how to use Kimi models with the Vercel AI SDK through Puter's OpenAI-compatible provider endpoint. No Moonshot AI API key needed — just your Puter auth token.

About Kimi

Kimi is built by Moonshot AI, a Chinese AI company known for long-context capabilities. Kimi models are designed for tasks that require processing large amounts of text, with context windows that can handle hundreds of thousands of tokens. The K2 family introduced strong reasoning and coding capabilities alongside the long-context strengths. Through Puter, you can access Kimi models via the Vercel AI SDK without needing a Moonshot AI API key.

Prerequisites

Puter copy auth token
  • Node.js installed on your machine

Setup

Install the Vercel AI SDK and the OpenAI provider:

npm install ai @ai-sdk/openai

Puter works as an OpenAI-compatible provider, so you use @ai-sdk/openai to connect. Configure it with Puter's base URL and your auth token:

import { createOpenAI } from '@ai-sdk/openai';

const puter = createOpenAI({
  baseURL: 'https://api.puter.com/puterai/openai/v1/',
  apiKey: 'YOUR_PUTER_AUTH_TOKEN',
});

Replace YOUR_PUTER_AUTH_TOKEN with the auth token you copied from your Puter dashboard. That's all you need. No Moonshot AI API key required.

Basic Text Generation

Here's a simple text generation call using Kimi K2:

import { createOpenAI } from '@ai-sdk/openai';
import { generateText } from 'ai';

const puter = createOpenAI({
  baseURL: 'https://api.puter.com/puterai/openai/v1/',
  apiKey: 'YOUR_PUTER_AUTH_TOKEN',
});

const { text } = await generateText({
  model: puter.chat('moonshotai/kimi-k2'),
  prompt: 'What is the capital of France?',
});

console.log(text);

The code is identical to what you'd write for any OpenAI provider. The only difference is the base URL and the model string.

Streaming

For longer responses, use streamText to get results in real-time:

import { createOpenAI } from '@ai-sdk/openai';
import { streamText } from 'ai';

const puter = createOpenAI({
  baseURL: 'https://api.puter.com/puterai/openai/v1/',
  apiKey: 'YOUR_PUTER_AUTH_TOKEN',
});

const result = streamText({
  model: puter.chat('moonshotai/kimi-k2'),
  prompt: 'Write a short story about a robot learning to paint.',
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}

Use streamText instead of generateText and iterate over result.textStream to get text chunks as they arrive.

Why Use Puter?

You could use Kimi through Moonshot AI's API directly. Here's why Puter is a simpler option:

  • One API key for everything — no need to sign up for separate Moonshot AI, Anthropic, or OpenAI accounts. Your Puter auth token covers all providers.
  • One setup for all models — the same Puter config works for Claude, GPT, Gemini, Llama, and 400+ other models. Just change the model string.
  • No extra packages — without Puter, each AI provider needs its own SDK package and API key. With Puter, everything goes through a single @ai-sdk/openai setup.

Conclusion

You now have the Moonshot provider set up through the Vercel AI SDK via Puter — no API key needed. Swap the model string to use any Kimi model, from the efficient Kimi Dev to the powerful Kimi K2.5, or any of the hundreds of other AI models available through Puter.

Free, Serverless AI and Cloud

Start creating powerful web applications with Puter.js in seconds!

Get Started Now

Read the Docs Try the Playground