v0-sdk 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +230 -0
- package/dist/index.js +105 -0
- package/package.json +25 -0
package/README.md
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
### v0 Chats API Client
|
|
2
|
+
|
|
3
|
+
A lightweight client for interacting with the v0 Chats API to create and manage AI-powered chat conversations.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```shellscript
|
|
8
|
+
npm install v0-sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add v0-sdk
|
|
11
|
+
# or
|
|
12
|
+
pnpm add v0-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
Get your API key from [v0.dev/chat/settings/keys](https://v0.dev/chat/settings/keys) and start creating chats.
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import { ChatsClient } from 'v0-sdk'
|
|
21
|
+
|
|
22
|
+
// Initialize with your API key
|
|
23
|
+
const client = new ChatsClient('your_v0_api_key')
|
|
24
|
+
|
|
25
|
+
// Create a new chat
|
|
26
|
+
const chat = await client.createChat({
|
|
27
|
+
message: 'Create a responsive navbar with Tailwind CSS',
|
|
28
|
+
system: 'You are an expert React developer',
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
console.log(`Chat created: ${chat.url}`)
|
|
32
|
+
console.log(`Generated code: ${chat.files.length} files`)
|
|
33
|
+
|
|
34
|
+
// Add a message to the chat
|
|
35
|
+
const response = await client.addMessage(chat.chatId, {
|
|
36
|
+
message: 'Add a dropdown menu to the navbar',
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
console.log(`AI response: ${response.text}`)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Authentication
|
|
43
|
+
|
|
44
|
+
The client requires a v0 API key, which you can create at [v0.dev/chat/settings/keys](https://v0.dev/chat/settings/keys).
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
// Initialize with API key
|
|
48
|
+
const client = new ChatsClient('your_v0_api_key')
|
|
49
|
+
|
|
50
|
+
// Or set it later
|
|
51
|
+
client.setApiKey('your_v0_api_key')
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API Reference
|
|
55
|
+
|
|
56
|
+
### Create a Chat
|
|
57
|
+
|
|
58
|
+
Create a new chat conversation with the AI.
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
const result = await client.createChat({
|
|
62
|
+
message: 'Create a login form with validation',
|
|
63
|
+
system: 'You are an expert in React and form validation',
|
|
64
|
+
chatPrivacy: 'private',
|
|
65
|
+
attachments: [{ url: 'https://example.com/design.png' }],
|
|
66
|
+
modelConfiguration: {
|
|
67
|
+
modelId: 'v0-1.5-md',
|
|
68
|
+
thinking: true,
|
|
69
|
+
imageGenerations: false,
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**Response:**
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
{
|
|
78
|
+
chatId: 'abc123',
|
|
79
|
+
url: 'https://v0.dev/chat/abc123',
|
|
80
|
+
text: 'I'll create a login form with validation...',
|
|
81
|
+
files: [
|
|
82
|
+
{
|
|
83
|
+
name: 'login-form.tsx',
|
|
84
|
+
content: '...',
|
|
85
|
+
type: 'component'
|
|
86
|
+
}
|
|
87
|
+
],
|
|
88
|
+
demo: 'https://v0.dev/demo/abc123'
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Add a Message
|
|
93
|
+
|
|
94
|
+
Add a message to an existing chat.
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
const response = await client.addMessage('abc123', {
|
|
98
|
+
message: 'Add password strength indicator',
|
|
99
|
+
attachments: [{ url: 'https://example.com/mockup.jpg' }],
|
|
100
|
+
modelConfiguration: {
|
|
101
|
+
thinking: true,
|
|
102
|
+
},
|
|
103
|
+
})
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Get Chat History
|
|
107
|
+
|
|
108
|
+
Coming soon
|
|
109
|
+
|
|
110
|
+
## Working with Attachments
|
|
111
|
+
|
|
112
|
+
The client supports both URL and base64-encoded attachments:
|
|
113
|
+
|
|
114
|
+
```javascript
|
|
115
|
+
// URL attachment
|
|
116
|
+
const response = await client.createChat({
|
|
117
|
+
message: 'Create a component based on this design',
|
|
118
|
+
attachments: [{ url: 'https://example.com/design.png' }],
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
// Base64 attachment (from file input)
|
|
122
|
+
document
|
|
123
|
+
.querySelector('input[type="file"]')
|
|
124
|
+
.addEventListener('change', async e => {
|
|
125
|
+
const file = e.target.files[0]
|
|
126
|
+
const reader = new FileReader()
|
|
127
|
+
|
|
128
|
+
reader.onload = async () => {
|
|
129
|
+
const response = await client.createChat({
|
|
130
|
+
message: 'Create a component based on this image',
|
|
131
|
+
attachments: [{ url: reader.result }],
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
console.log(response)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
reader.readAsDataURL(file)
|
|
138
|
+
})
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Customizing Model Behavior
|
|
142
|
+
|
|
143
|
+
Control the AI model's behavior with the `modelConfiguration` parameter:
|
|
144
|
+
|
|
145
|
+
```javascript
|
|
146
|
+
const response = await client.createChat({
|
|
147
|
+
message: 'Create a dashboard layout',
|
|
148
|
+
modelConfiguration: {
|
|
149
|
+
// Use a specific model version
|
|
150
|
+
modelId: 'v0-1.5-md',
|
|
151
|
+
|
|
152
|
+
// Enable AI-generated images in the response
|
|
153
|
+
imageGenerations: true,
|
|
154
|
+
|
|
155
|
+
// Show the AI's reasoning process
|
|
156
|
+
thinking: true,
|
|
157
|
+
},
|
|
158
|
+
})
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Error Handling
|
|
162
|
+
|
|
163
|
+
The client throws descriptive errors for various failure scenarios:
|
|
164
|
+
|
|
165
|
+
```javascript
|
|
166
|
+
try {
|
|
167
|
+
const chat = await client.createChat({
|
|
168
|
+
message: 'Create a component',
|
|
169
|
+
})
|
|
170
|
+
} catch (error) {
|
|
171
|
+
if (error.status === 403) {
|
|
172
|
+
console.error('Authentication error:', error.message)
|
|
173
|
+
} else if (error.status === 429) {
|
|
174
|
+
console.error('Rate limit exceeded:', error.message)
|
|
175
|
+
} else {
|
|
176
|
+
console.error('API error:', error.message)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Advanced Usage
|
|
182
|
+
|
|
183
|
+
### Streaming Responses
|
|
184
|
+
|
|
185
|
+
Coming soon
|
|
186
|
+
|
|
187
|
+
### Team Collaboration
|
|
188
|
+
|
|
189
|
+
Share chats with your team:
|
|
190
|
+
|
|
191
|
+
```javascript
|
|
192
|
+
const chat = await client.createChat({
|
|
193
|
+
message: 'Create a component for our design system',
|
|
194
|
+
chatPrivacy: 'team-edit',
|
|
195
|
+
teamId: 'your-team-id',
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
// Now team members can view and edit this chat
|
|
199
|
+
console.log(`Team chat URL: ${chat.url}`)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Troubleshooting
|
|
203
|
+
|
|
204
|
+
### Rate Limiting
|
|
205
|
+
|
|
206
|
+
The API has rate limits of 100 requests per minute and 1,000 requests per hour. If you hit these limits, you'll receive a 429 status code.
|
|
207
|
+
|
|
208
|
+
### Common Errors
|
|
209
|
+
|
|
210
|
+
- **403 Forbidden**: Check your API key and ensure you have a valid subscription.
|
|
211
|
+
- **400 Bad Request**: Verify your request parameters match the API requirements.
|
|
212
|
+
- **404 Not Found**: Ensure the chat ID exists and is accessible to your API key.
|
|
213
|
+
- **413 Payload Too Large**: Reduce attachment size (max 10MB per attachment).
|
|
214
|
+
|
|
215
|
+
### Debugging
|
|
216
|
+
|
|
217
|
+
Enable debug mode to see detailed request and response information:
|
|
218
|
+
|
|
219
|
+
```javascript
|
|
220
|
+
const client = new ChatsClient('your_api_key', undefined, { debug: true })
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Resources
|
|
224
|
+
|
|
225
|
+
- [v0 Documentation](https://v0.dev/docs)
|
|
226
|
+
- [API Terms](https://vercel.com/legal/api-terms)
|
|
227
|
+
|
|
228
|
+
## License
|
|
229
|
+
|
|
230
|
+
MIT
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v0 Chats API SDK
|
|
3
|
+
* A client library for interacting with the v0 Chats API
|
|
4
|
+
*/ class V0ChatsError extends Error {
|
|
5
|
+
constructor(message, status, data){
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'V0ChatsError';
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.data = data;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Client for the v0 Chats API
|
|
14
|
+
*/ class V0Client {
|
|
15
|
+
/**
|
|
16
|
+
* Create a new v0 Chats API client
|
|
17
|
+
*
|
|
18
|
+
* @param options Client configuration options
|
|
19
|
+
*/ constructor(options){
|
|
20
|
+
this.apiKey = options.apiKey;
|
|
21
|
+
this.baseUrl = options.baseUrl || 'https://api.v0.dev/api';
|
|
22
|
+
this.fetchFn = options.fetch || fetch;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Create a new chat
|
|
26
|
+
*
|
|
27
|
+
* @param options Options for creating a new chat
|
|
28
|
+
* @returns Promise with the created chat details
|
|
29
|
+
*/ async createChat(options) {
|
|
30
|
+
const response = await this.request('POST', '/chats', options);
|
|
31
|
+
return response;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Add a message to an existing chat
|
|
35
|
+
*
|
|
36
|
+
* @param chatId The ID of the chat to add a message to
|
|
37
|
+
* @param options Options for adding a message
|
|
38
|
+
* @returns Promise with the updated chat details
|
|
39
|
+
*/ async addMessage(chatId, options) {
|
|
40
|
+
const response = await this.request('POST', `/chats/${chatId}`, options);
|
|
41
|
+
return response;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get chat details and message history
|
|
45
|
+
*
|
|
46
|
+
* @param chatId The ID of the chat to retrieve
|
|
47
|
+
* @returns Promise with the chat details and messages
|
|
48
|
+
*/ async getChat(chatId) {
|
|
49
|
+
const response = await this.request('GET', `/chats/${chatId}`);
|
|
50
|
+
return response;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Continue an existing chat (deprecated)
|
|
54
|
+
*
|
|
55
|
+
* This method is deprecated. Use addMessage() instead.
|
|
56
|
+
*
|
|
57
|
+
* @deprecated Use addMessage() instead
|
|
58
|
+
* @param chatId The ID of the chat to continue
|
|
59
|
+
* @param options Options for continuing the chat
|
|
60
|
+
* @returns Promise with the updated chat details
|
|
61
|
+
*/ async continueChat(chatId, options) {
|
|
62
|
+
console.warn('continueChat() is deprecated. Use addMessage() instead.');
|
|
63
|
+
const payload = {
|
|
64
|
+
...options,
|
|
65
|
+
chatId
|
|
66
|
+
};
|
|
67
|
+
const response = await this.request('POST', '/chats', payload);
|
|
68
|
+
return response;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Make a request to the v0 Chats API
|
|
72
|
+
*
|
|
73
|
+
* @param method HTTP method
|
|
74
|
+
* @param path API path
|
|
75
|
+
* @param data Request data
|
|
76
|
+
* @returns Promise with the response data
|
|
77
|
+
*/ async request(method, path, data) {
|
|
78
|
+
const url = `${this.baseUrl}${path}`;
|
|
79
|
+
const headers = {
|
|
80
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
81
|
+
'Content-Type': 'application/json'
|
|
82
|
+
};
|
|
83
|
+
const options = {
|
|
84
|
+
method,
|
|
85
|
+
headers,
|
|
86
|
+
body: data && method !== 'GET' ? JSON.stringify(data) : undefined
|
|
87
|
+
};
|
|
88
|
+
try {
|
|
89
|
+
const response = await this.fetchFn(url, options);
|
|
90
|
+
const responseData = await response.json();
|
|
91
|
+
if (!response.ok) {
|
|
92
|
+
throw new V0ChatsError(responseData.error || `API request failed with status ${response.status}`, response.status, responseData);
|
|
93
|
+
}
|
|
94
|
+
return responseData;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (error instanceof V0ChatsError) {
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
throw new V0ChatsError(error instanceof Error ? error.message : 'Unknown error occurred', 500);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
exports.V0ChatsError = V0ChatsError;
|
|
105
|
+
exports.V0Client = V0Client;
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "v0-sdk",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "TypeScript SDK for the v0 Chats API",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"keywords": [
|
|
10
|
+
"v0",
|
|
11
|
+
"vercel",
|
|
12
|
+
"ai",
|
|
13
|
+
"chat",
|
|
14
|
+
"sdk"
|
|
15
|
+
],
|
|
16
|
+
"author": "",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"bunchee": "^6.5.2",
|
|
20
|
+
"typescript": "5.7.3"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "bunchee"
|
|
24
|
+
}
|
|
25
|
+
}
|