@thinksoftai/sdk
v1.3.1
Published
Official JavaScript/TypeScript SDK for ThinkSoft AI Developer API
Maintainers
Readme
@thinksoftai/sdk
Official JavaScript/TypeScript SDK for ThinkSoft Developer API.
Installation
npm install @thinksoftai/sdkQuick Start
import { ThinkSoft } from '@thinksoftai/sdk'
// Initialize client
const client = new ThinkSoft({
appId: 'your-app-id'
})
// Authenticate user
await client.auth.sendOtp('[email protected]')
const { token, user } = await client.auth.verifyOtp('[email protected]', '123456')
// Fetch data
const contacts = await client.from('contacts').list()Authentication
Login with ThinkSoft (Email OTP)
// Send OTP to user's email
const result = await client.auth.sendOtp('[email protected]')
if (!result.success) {
console.error(result.error)
}
// Verify OTP and get token
const auth = await client.auth.verifyOtp('[email protected]', '123456')
if (auth.success) {
console.log('User:', auth.user)
// Token is automatically stored and used for subsequent requests
}Using API Key (Backend)
const client = new ThinkSoft({
appId: 'your-app-id',
token: 'ts_secret_your_api_key' // API key for server-side usage
})Manual Token Management
// Set token manually
client.auth.setToken('your-token')
// Get current token
const token = client.auth.getToken()
// Get current user
const user = client.auth.getUser()
// Check authentication status
if (client.auth.isAuthenticated()) {
// User is logged in
}
// Logout
client.auth.logout()Token Refresh (Auto & Manual)
The SDK automatically refreshes tokens before they expire. You can also manually check and refresh:
// Check if token is expired (with 5-minute buffer)
if (client.auth.isTokenExpired()) {
console.log('Token expired or expiring soon')
}
// Manually refresh session
const result = await client.auth.refreshSession()
if (result.success) {
console.log('Token refreshed!')
}
// Ensure valid token before operations
const isValid = await client.auth.ensureValidToken()Note: Auto-refresh happens automatically before API calls when the token is within 5 minutes of expiry.
Create Apps with AI
Create new apps dynamically using natural language prompts:
// Create a new app using AI
const result = await client.createApp({
prompt: 'Create a CRM app for Sales Department with leads, contacts, and deals tracking'
})
if (result.success) {
console.log('App created:', result.app.name)
console.log('App ID:', result.app.app_id)
console.log('URL:', result.app.url)
}
// Create a store app
const storeResult = await client.createApp({
prompt: 'Create an e-commerce store for selling electronics',
type: 'storeapp'
})The created app will be available in your ThinkSoft dashboard under "My Apps".
CRUD Operations
List Records
// List all
const { data, count } = await client.from('contacts').list()
// With filtering
const { data } = await client.from('contacts').list({
filter: { status: 'active' },
sort: 'created_at:desc',
limit: 10,
offset: 0
})Get Single Record
const contact = await client.from('contacts').get('record-id')Create Record
const newContact = await client.from('contacts').create({
name: 'John Doe',
email: '[email protected]'
})Update Record
const updated = await client.from('contacts').update('record-id', {
name: 'Jane Doe'
})Delete Record
await client.from('contacts').delete('record-id')Helper Methods
// Find first matching record
const contact = await client.from('contacts').findFirst({ email: '[email protected]' })
// Count records
const total = await client.from('contacts').count()
const activeCount = await client.from('contacts').count({ status: 'active' })Database & Schema
// Get all tables in the database
const tables = await client.tables()
// [{ id, slug, name, icon, fieldCount }]
// Get table schema
const schema = await client.from('contacts').schema()
// { id, slug, name, fields: [...] }
// Get table fields only
const fields = await client.from('contacts').fields()
// [{ name, label, type, required, options }]TypeScript
The SDK is fully typed. You can provide types for your data:
interface Contact {
id: string
name: string
email: string
status: 'active' | 'inactive'
}
// Typed queries
const contacts = await client.from<Contact>('contacts').list()
// contacts.data is Contact[]
const contact = await client.from<Contact>('contacts').get('id')
// contact is ContactConfiguration
const client = new ThinkSoft({
// Required
appId: 'your-app-id',
// Optional
baseUrl: 'https://thinksoftai.com/api/v1', // Custom API URL
token: 'your-token', // Pre-set auth token
storage: 'localStorage' // 'localStorage' | 'memory' | 'none'
})React Example
import { ThinkSoft } from '@thinksoftai/sdk'
import { useState, useEffect } from 'react'
const client = new ThinkSoft({ appId: 'xxx' })
function ContactsList() {
const [contacts, setContacts] = useState([])
const [loading, setLoading] = useState(true)
useEffect(() => {
client.from('contacts')
.list({ sort: 'created_at:desc' })
.then(({ data }) => setContacts(data))
.finally(() => setLoading(false))
}, [])
if (loading) return <div>Loading...</div>
return (
<ul>
{contacts.map(c => (
<li key={c.id}>{c.name}</li>
))}
</ul>
)
}Node.js Example
const { ThinkSoft } = require('@thinksoftai/sdk')
const client = new ThinkSoft({
appId: 'xxx',
token: process.env.THINKSOFT_API_KEY,
storage: 'memory' // Use memory storage in Node.js
})
async function main() {
const { data: contacts } = await client.from('contacts').list()
console.log(contacts)
}
main()License
MIT
