Supabase API tools for OpenAI, Anthropic, and AI SDK
npm install @tooly/supabaseSupabase API tools for AI applications, compatible with OpenAI function calling, Anthropic tool use, and AI SDK.
``bash`
npm install @tooly/supabaseor
yarn add @tooly/supabaseor
pnpm add @tooly/supabase
Get your Supabase URL and API key from your Supabase project dashboard.
`typescript
import { SupabaseTools } from '@tooly/supabase'
const supabase = new SupabaseTools('your-supabase-url', 'your-supabase-anon-key')
// Get available tools for function calling
const tools = supabase.getTools()
// Execute a function
const result = await supabase.executeFunction('selectData', {
table: 'users',
columns: ['id', 'name', 'email'],
limit: 10,
})
`
`typescript
import { createAITools } from '@tooly/supabase'
const tools = createAITools('your-supabase-url', 'your-supabase-anon-key')
// Use with generateText
import { generateText } from 'ai'
const result = await generateText({
model: openai('gpt-4-turbo'),
messages: [{ role: 'user', content: 'Show me all users from the database' }],
tools,
})
`
`typescript
import { createOpenAIFunctions } from '@tooly/supabase'
const { tools, executeFunction } = createOpenAIFunctions('your-supabase-url', 'your-supabase-anon-key')
// Use with OpenAI client
const completion = await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages: [{ role: 'user', content: 'Create a new user in the database' }],
tools,
})
// Execute function calls
for (const toolCall of completion.choices[0].message.tool_calls || []) {
const result = await executeFunction(toolCall.function.name, JSON.parse(toolCall.function.arguments))
}
`
`typescript
import { createAnthropicTools } from '@tooly/supabase'
const { tools, executeFunction } = createAnthropicTools('your-supabase-url', 'your-supabase-anon-key')
// Use with Anthropic client
const message = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20241022',
messages: [{ role: 'user', content: 'Upload a file to storage' }],
tools,
})
// Execute tool calls
for (const toolUse of message.content.filter((c) => c.type === 'tool_use')) {
const result = await executeFunction(toolUse.name, toolUse.input)
}
`
- selectData - Query data from tables with filters, sorting, and paginationinsertData
- - Insert new records into tablesupdateData
- - Update existing records in tablesdeleteData
- - Delete records from tablesupsertData
- - Insert or update records (upsert operation)
- signUp - Create a new user accountsignIn
- - Sign in a user with email and passwordsignOut
- - Sign out the current usergetUser
- - Get current authenticated user information
- uploadFile - Upload files to Supabase StoragedownloadFile
- - Download files from Supabase StoragelistFiles
- - List files in storage bucketscreateBucket
- - Create new storage buckets
`typescript
// Select data with filters
await supabase.executeFunction('selectData', {
table: 'posts',
columns: ['id', 'title', 'created_at'],
filters: [
{ column: 'published', operator: 'eq', value: true },
{ column: 'created_at', operator: 'gte', value: '2024-01-01' },
],
orderBy: { column: 'created_at', ascending: false },
limit: 20,
})
// Insert new data
await supabase.executeFunction('insertData', {
table: 'users',
data: { name: 'John Doe', email: 'john@example.com' },
})
// Update data
await supabase.executeFunction('updateData', {
table: 'users',
data: { name: 'Jane Doe' },
filters: [{ column: 'id', operator: 'eq', value: 123 }],
})
`
`typescript
// Sign up new user
await supabase.executeFunction('signUp', {
email: 'user@example.com',
password: 'secure-password',
options: {
data: { full_name: 'John Doe' },
emailRedirectTo: 'https://example.com/welcome',
},
})
// Sign in user
await supabase.executeFunction('signIn', {
email: 'user@example.com',
password: 'secure-password',
})
`
`typescript
// Upload file
await supabase.executeFunction('uploadFile', {
bucket: 'avatars',
path: 'user-123/profile.jpg',
file: fileData,
options: {
contentType: 'image/jpeg',
upsert: true,
},
})
// List files
await supabase.executeFunction('listFiles', {
bucket: 'documents',
path: 'folder/',
options: {
limit: 50,
sortBy: { column: 'created_at', order: 'desc' },
},
})
``
MIT