ExamplesAPI Integration Guide

API Integration Guide

Integrate GPU-powered AI inference into existing applications with simple REST API calls or SDK integration.

IAAS Simplicity: Deploy once, use from any language. No GPU expertise required.

Use Cases

  • E-commerce: Product description generation
  • SaaS Apps: AI-powered features
  • Mobile Apps: Backend AI API
  • WordPress/Shopify: Plugin integration
  • Zapier/Make: Workflow automation

Quick Start (5 Minutes)

Deploy Pre-configured Template

# One command - no GPU configuration needed
syaala templates deploy sentiment-analysis --name my-api

Get API Endpoint

syaala deployments get my-api
# Output: https://my-api-abc123.run.syaala.com

Make API Call

curl -X POST https://my-api-abc123.run.syaala.com/v1/completions \
  -H "Authorization: Bearer $SYAALA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Analyze sentiment: The product quality exceeded my expectations!",
    "max_tokens": 10
  }'

Integration Patterns

Pattern 1: Direct REST API

Best for: Any language, quick integration, serverless functions

// No SDK required - just fetch()
const response = await fetch('https://my-deployment.run.syaala.com/v1/completions', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.SYAALA_API_KEY}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    prompt: 'Your prompt here',
    max_tokens: 100,
    temperature: 0.7
  })
})
 
const data = await response.json()
console.log(data.choices[0].text)

Pattern 2: TypeScript SDK

Best for: Node.js apps, TypeScript projects, type safety

import { createClient } from '@syaala/sdk'
 
const client = createClient(process.env.SYAALA_API_KEY!)
 
const result = await client.inference.complete(deploymentId, {
  prompt: 'Your prompt here',
  maxTokens: 100
})
 
if (result.success) {
  console.log(result.data.text)
}

Pattern 3: Webhook Integration

Best for: Asynchronous processing, long-running tasks

// Configure webhook endpoint
await client.deployments.update(deploymentId, {
  webhookUrl: 'https://your-app.com/api/webhooks/inference',
  webhookEvents: ['inference.completed', 'inference.failed']
})
 
// Your webhook handler
app.post('/api/webhooks/inference', async (req, res) => {
  const { event, data } = req.body
  
  if (event === 'inference.completed') {
    await processResult(data.result)
  }
  
  res.status(200).send('OK')
})

Real-World Examples

E-Commerce: Product Description Generator

// Shopify App Integration
import { createClient } from '@syaala/sdk'
import Shopify from '@shopify/shopify-api'
 
const syaala = createClient(process.env.SYAALA_API_KEY!)
const DEPLOYMENT_ID = 'dep_product_descriptions'
 
app.post('/api/generate-description', async (req, res) => {
  const { productTitle, features, category } = req.body
  
  const prompt = `Generate a compelling product description:
Title: ${productTitle}
Category: ${category}
Features: ${features.join(', ')}
 
Description:`
  
  const result = await syaala.inference.complete(DEPLOYMENT_ID, {
    prompt,
    maxTokens: 200,
    temperature: 0.8
  })
  
  if (!result.success) {
    return res.status(500).json({ error: result.error })
  }
  
  // Update Shopify product
  const shop = await Shopify.Context.loadOfflineSession(req.query.shop)
  const client = new Shopify.Clients.Rest(shop.shop, shop.accessToken)
  
  await client.put({
    path: `products/${req.query.productId}`,
    data: {
      product: {
        body_html: result.data.text
      }
    }
  })
  
  res.json({ description: result.data.text })
})

SaaS: Email Response Generator

// Gmail Add-on Integration
import { createClient } from '@syaala/sdk'
import { google } from 'googleapis'
 
const syaala = createClient(process.env.SYAALA_API_KEY!)
const DEPLOYMENT_ID = 'dep_email_assistant'
 
async function generateEmailReply(emailThread: string, tone: string) {
  const prompt = `Generate a professional ${tone} email reply:
 
Original Email:
${emailThread}
 
Reply:`
  
  const result = await syaala.inference.complete(DEPLOYMENT_ID, {
    prompt,
    maxTokens: 300,
    temperature: 0.7
  })
  
  return result.success ? result.data.text : null
}
 
// Google Workspace Add-on
function onGmailMessage(e: any) {
  const messageId = e.gmail.messageId
  const gmail = google.gmail({ version: 'v1', auth: e.auth })
  
  gmail.users.messages.get({
    userId: 'me',
    id: messageId
  }, async (err, message) => {
    const emailContent = message.data.snippet
    const reply = await generateEmailReply(emailContent, 'professional')
    
    // Show reply in sidebar
    return CardService.newCardBuilder()
      .setHeader(CardService.newCardHeader().setTitle('AI Reply'))
      .addSection(
        CardService.newCardSection()
          .addWidget(CardService.newTextParagraph().setText(reply))
      )
      .build()
  })
}

WordPress: Content Summarizer Plugin

<?php
// WordPress Plugin: AI Content Summarizer
 
function syaala_summarize_post($post_content) {
    $api_key = get_option('syaala_api_key');
    $deployment_id = get_option('syaala_deployment_id');
    
    $response = wp_remote_post("https://platform.syaala.com/api/inference/{$deployment_id}/complete", [
        'headers' => [
            'Authorization' => "Bearer {$api_key}",
            'Content-Type' => 'application/json'
        ],
        'body' => json_encode([
            'prompt' => "Summarize this article in 2-3 sentences:\n\n" . $post_content . "\n\nSummary:",
            'max_tokens' => 150
        ])
    ]);
    
    if (is_wp_error($response)) {
        return '';
    }
    
    $data = json_decode(wp_remote_retrieve_body($response), true);
    return $data['text'] ?? '';
}
 
// Add meta box to post editor
add_action('add_meta_boxes', function() {
    add_meta_box(
        'syaala_summary',
        'AI Summary',
        'syaala_summary_metabox',
        'post',
        'side'
    );
});
 
function syaala_summary_metabox($post) {
    $summary = get_post_meta($post->ID, '_ai_summary', true);
    
    if (empty($summary)) {
        $summary = syaala_summarize_post($post->post_content);
        update_post_meta($post->ID, '_ai_summary', $summary);
    }
    
    echo '<p>' . esc_html($summary) . '</p>';
    echo '<button type="button" onclick="regenerateSummary()">Regenerate</button>';
}
?>

Python: Flask API Integration

from flask import Flask, request, jsonify
import os
import requests
 
app = Flask(__name__)
 
SYAALA_API_KEY = os.environ['SYAALA_API_KEY']
DEPLOYMENT_ID = os.environ['DEPLOYMENT_ID']
 
@app.route('/api/classify', methods=['POST'])
def classify_text():
    """Classify user input using Syaala deployment"""
    data = request.json
    text = data.get('text', '')
    
    # Call Syaala API
    response = requests.post(
        f'https://platform.syaala.com/api/inference/{DEPLOYMENT_ID}/complete',
        headers={
            'Authorization': f'Bearer {SYAALA_API_KEY}',
            'Content-Type': 'application/json'
        },
        json={
            'prompt': f'Classify this text: "{text}"\nCategory:',
            'max_tokens': 20,
            'temperature': 0.3
        }
    )
    
    if response.status_code != 200:
        return jsonify({'error': 'Classification failed'}), 500
    
    result = response.json()
    category = result['text'].strip()
    
    return jsonify({
        'text': text,
        'category': category,
        'confidence': result.get('confidence', 0.95)
    })
 
if __name__ == '__main__':
    app.run(port=5000)

Mobile: React Native Integration

// React Native App
import { useState } from 'react'
import { View, TextInput, Button, Text } from 'react-native'
 
const SYAALA_API_KEY = process.env.SYAALA_API_KEY
const DEPLOYMENT_ID = 'dep_mobile_assistant'
 
function AIAssistant() {
  const [input, setInput] = useState('')
  const [response, setResponse] = useState('')
  const [loading, setLoading] = useState(false)
  
  async function getAIResponse() {
    setLoading(true)
    try {
      const res = await fetch(
        `https://platform.syaala.com/api/inference/${DEPLOYMENT_ID}/complete`,
        {
          method: 'POST',
          headers: {
            'Authorization': `Bearer ${SYAALA_API_KEY}`,
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            prompt: input,
            max_tokens: 200
          })
        }
      )
      
      const data = await res.json()
      setResponse(data.text)
    } catch (error) {
      console.error('AI error:', error)
    } finally {
      setLoading(false)
    }
  }
  
  return (
    <View>
      <TextInput 
        value={input}
        onChangeText={setInput}
        placeholder="Ask AI anything..."
      />
      <Button 
        title={loading ? 'Processing...' : 'Get Response'}
        onPress={getAIResponse}
        disabled={loading}
      />
      {response && <Text>{response}</Text>}
    </View>
  )
}

Zapier Integration

Create custom Zapier integration:

// Zapier App Definition
const App = {
  version: '1.0.0',
  platformVersion: '1.0.0',
  
  authentication: {
    type: 'custom',
    fields: [
      { key: 'apiKey', label: 'Syaala API Key', required: true, type: 'string' }
    ],
    test: async (z: any, bundle: any) => {
      const response = await z.request({
        url: 'https://platform.syaala.com/api/profile',
        headers: {
          'Authorization': `Bearer ${bundle.authData.apiKey}`
        }
      })
      return response.data
    }
  },
  
  triggers: {},
  
  creates: {
    generate_text: {
      key: 'generate_text',
      noun: 'Text',
      display: {
        label: 'Generate Text with AI',
        description: 'Generate text using your Syaala deployment'
      },
      operation: {
        inputFields: [
          { key: 'deployment_id', required: true, label: 'Deployment ID' },
          { key: 'prompt', required: true, label: 'Prompt' },
          { key: 'max_tokens', type: 'integer', default: 100, label: 'Max Tokens' }
        ],
        perform: async (z: any, bundle: any) => {
          const response = await z.request({
            url: `https://platform.syaala.com/api/inference/${bundle.inputData.deployment_id}/complete`,
            method: 'POST',
            headers: {
              'Authorization': `Bearer ${bundle.authData.apiKey}`,
              'Content-Type': 'application/json'
            },
            body: {
              prompt: bundle.inputData.prompt,
              max_tokens: bundle.inputData.max_tokens
            }
          })
          
          return response.data
        }
      }
    }
  }
}

Best Practices

1. Error Handling

async function robustInference(prompt: string, retries = 3) {
  for (let i = 0; i < retries; i++) {
    try {
      const result = await client.inference.complete(deploymentId, {
        prompt,
        maxTokens: 200
      })
      
      if (result.success) {
        return result.data.text
      }
      
      // Rate limit - wait and retry
      if (result.error.includes('rate limit')) {
        await new Promise(r => setTimeout(r, Math.pow(2, i) * 1000))
        continue
      }
      
      throw new Error(result.error)
    } catch (error) {
      if (i === retries - 1) throw error
      await new Promise(r => setTimeout(r, 1000))
    }
  }
}

2. Response Caching

import Redis from 'ioredis'
 
const redis = new Redis(process.env.REDIS_URL)
 
async function cachedInference(prompt: string) {
  // Check cache
  const cached = await redis.get(`inference:${prompt}`)
  if (cached) {
    return JSON.parse(cached)
  }
  
  // Call API
  const result = await client.inference.complete(deploymentId, { prompt })
  
  if (result.success) {
    // Cache for 1 hour
    await redis.setex(`inference:${prompt}`, 3600, JSON.stringify(result.data))
    return result.data
  }
  
  throw new Error(result.error)
}

3. Rate Limiting

import { Ratelimit } from '@upstash/ratelimit'
import { Redis } from '@upstash/redis'
 
const ratelimit = new Ratelimit({
  redis: Redis.fromEnv(),
  limiter: Ratelimit.slidingWindow(100, '1 m') // 100 requests per minute
})
 
app.post('/api/inference', async (req, res) => {
  const identifier = req.headers['x-forwarded-for'] || 'anonymous'
  const { success } = await ratelimit.limit(identifier)
  
  if (!success) {
    return res.status(429).json({ error: 'Too many requests' })
  }
  
  // Process inference...
})

4. Cost Monitoring

class CostTracker {
  async logInference(deploymentId: string, tokens: number) {
    await db.insert('inference_logs', {
      deployment_id: deploymentId,
      tokens_used: tokens,
      cost: tokens * 0.00002, // $0.02 per 1K tokens
      timestamp: new Date()
    })
  }
  
  async getDailyCost(): Promise<number> {
    const result = await db.query(`
      SELECT SUM(cost) as total
      FROM inference_logs
      WHERE timestamp >= NOW() - INTERVAL '1 day'
    `)
    return result[0].total || 0
  }
}

Deployment Templates

Use pre-configured templates for instant deployment:

# List available templates
syaala templates list
 
# Common templates:
syaala templates deploy sentiment-analysis --name my-sentiment-api
syaala templates deploy text-summarization --name my-summary-api
syaala templates deploy content-moderation --name my-moderation-api
syaala templates deploy translation --name my-translation-api
syaala templates deploy entity-extraction --name my-entities-api

Each template includes:

  • ✅ Pre-configured runtime (vLLM/Triton)
  • ✅ Optimized GPU selection
  • ✅ Auto-scaling settings
  • ✅ Environment variables
  • ✅ Health checks
  • ✅ Ready-to-use API endpoint

Monitoring

# Set up webhook for alerts
syaala notifications create \
  --type webhook \
  --config '{"url": "https://your-app.com/api/alerts"}'
 
# Create alerts
syaala alerts create \
  --deployment dep_abc123 \
  --metric error_rate \
  --threshold 0.05 \
  --action notify

Next Steps