
How to Use Vercel AI SDK
A practical guide to get you up and running with Vercel AI SDK. Written by Delv Editorial, Delv Team.
Getting started with Vercel AI SDK
In this guide, you'll learn how to set up the Vercel AI SDK and create your first AI-powered application using TypeScript. By the end, you'll be able to build applications that can call tools and manage multi-step workflows.
Step 1: Sign up and set up
- Go to the Vercel AI SDK website.
- Click on the Get Started button on the homepage.
- If you don't have an account, click on Sign Up and create a free account using your email or GitHub.
- After signing up, log in to your account.
- Click on Documentation in the top menu to access the SDK documentation for setup instructions.
Step 2: Your first application
- Open your terminal and create a new directory for your project:
mkdir my-ai-app
cd my-ai-app
- Initialise a new TypeScript project:
npm init -y
npm install typescript ts-node @types/node --save-dev
npx tsc --init
- Install the Vercel AI SDK:
npm install @vercel/ai-sdk
- Create a new file named
index.tsin your project directory. - Add the following code to
index.tsto set up a basic AI tool call:
import { createAI } from '@vercel/ai-sdk';
const ai = createAI({ apiKey: 'your-api-key-here' });
async function fetchResponse() {
const response = await ai.call('What is the weather today?');
console.log(response);
}
fetchResponse();
- Replace
'your-api-key-here'with your actual API key from the Vercel dashboard. - Run your application:
npx ts-node index.ts
Step 3: Get better results
- To enhance your AI's responses, use specific prompts. Instead of asking vague questions, frame them with context, like "Explain the weather patterns in London today."
- Explore the API Reference in the documentation to learn about additional methods and settings you can configure for your AI calls.
Pro tip
Use TypeScript's type definitions by installing @types/vercel__ai-sdk to get autocomplete suggestions and error checking while coding. This will speed up your development process and help you avoid mistakes.
Common mistake to avoid
Many beginners forget to add their API key, leading to authentication errors. Ensure you have correctly set your API key in the configuration before running your application.