@launchbasex/sdk
v1.0.0
Published
LaunchBase SDK for connecting generated backends to LaunchBase platform services
Downloads
104
Maintainers
Readme
@launchbasex/sdk
Official SDK for connecting generated backends to LaunchBase platform services.
Installation
npm install @launchbasex/sdkQuick Start
import LaunchBase from '@launchbasex/sdk'
const client = new LaunchBase({
apiKey: process.env.LAUNCHBASE_API_KEY,
projectId: process.env.LAUNCHBASE_PROJECT_ID,
})
// Health check
await client.healthCheck()
// Get project info
const project = await client.getProject()Authentication
// Sign up
const { user, token } = await client.auth.signUp('[email protected]', 'password', {
name: 'John Doe',
})
// Sign in
const auth = await client.auth.signIn('[email protected]', 'password')
// OAuth
const { url } = await client.auth.signInWithProvider('google')
// Verify token
const payload = await client.auth.verifyToken(token)
// Refresh token
const newAuth = await client.auth.refreshToken(auth.refreshToken)Database
// Query
const users = await client.database.query('SELECT * FROM users WHERE active = $1', [true])
// CRUD
const user = await client.database.create('users', { email: '[email protected]', name: 'Test' })
const found = await client.database.findUnique('users', user.id)
const updated = await client.database.update('users', user.id, { name: 'Updated' })
await client.database.delete('users', user.id)
// Query with options
const activeUsers = await client.database.findMany('users', {
where: { active: true },
orderBy: { createdAt: 'desc' },
take: 10,
})
// Transaction
await client.database.transaction(async (tx) => {
const user = await tx.create('users', { email: '[email protected]' })
await tx.create('profiles', { userId: user.id, bio: 'Hello' })
})Storage
// Upload file
const file = await client.storage.upload('avatars/user.jpg', buffer, {
contentType: 'image/jpeg',
public: true,
})
// Download
const data = await client.storage.download('avatars/user.jpg')
// Get signed URL
const url = await client.storage.getSignedUrl('private/doc.pdf', 3600)
// List files
const files = await client.storage.list('avatars/')
// Delete
await client.storage.delete('avatars/user.jpg')Realtime
// Subscribe to channel
const unsubscribe = client.realtime.subscribe('chat', (event) => {
console.log('New message:', event.data)
})
// Publish event
await client.realtime.publish('chat', {
type: 'message',
data: { text: 'Hello world!' },
})
// Unsubscribe
unsubscribe()Webhooks
// Create webhook
const webhook = await client.webhooks.create('https://example.com/webhook', [
'user.created',
'user.updated',
])
// List webhooks
const webhooks = await client.webhooks.list()
// Verify incoming webhook signature
import { WebhookClient } from '@launchbase/sdk/webhooks'
const { valid, event } = WebhookClient.constructEvent(
payload,
signature,
webhook.secret,
timestamp
)
if (valid) {
console.log('Event:', event)
}Configuration
| Option | Required | Default | Description |
|--------|----------|---------|-------------|
| apiKey | Yes | - | Your project API key |
| projectId | Yes | - | Your project ID |
| apiUrl | No | https://api.launchbase.dev | API base URL |
| timeout | No | 30000 | Request timeout in ms |
| retries | No | 3 | Number of retries on failure |
Environment Variables
LAUNCHBASE_API_KEY=your_api_key
LAUNCHBASE_PROJECT_ID=your_project_id
LAUNCHBASE_API_URL=https://api.launchbase.dev # optionalLicense
MIT
