JavaScript/TypeScript Examples

Complete JavaScript and TypeScript examples for all Tenzro services. These examples work in both Node.js and browser environments with full TypeScript support.

Setup & Installation

Copy and paste these examples into your JavaScript/TypeScript project

tenzro-example.js
// Install the Tenzro JavaScript SDK
npm install @tenzro/sdk

// Import and initialize
import { Tenzro } from '@tenzro/sdk';

const tenzro = new Tenzro({
  apiKey: 'sk_your_key_here', // Get from platform.tenzro.com
  baseURL: 'https://api.tenzro.com', // Optional, defaults to this
});

// TypeScript support is built-in
interface GenerateResponse {
  content: string;
  model: string;
  tokens_used: number;
}

// Example usage
async function main() {
  try {
    const response = await tenzro.cortex.generate({
      prompt: "Hello, Tenzro!",
      model: "gpt-4o"
    });
    
    console.log('Generated text:', response.content);
  } catch (error) {
    console.error('Error:', error.message);
  }
}

main();

Environment Support

Node.js

Server-side applications, APIs, and backend services

Node.js 16+

Browser

React, Vue, Angular, and vanilla JavaScript applications

ES2020+

Edge Runtime

Vercel Edge, Cloudflare Workers, and Deno Deploy

Web APIs

Framework Examples

⚛️ React/Next.js

useTenzro.ts
// hooks/useTenzro.ts
import { useState } from 'react'
import { Tenzro } from '@tenzro/sdk'

export function useTenzro(apiKey: string) {
  const [tenzro] = useState(() => new Tenzro({ apiKey }))
  return tenzro
}

🟢 Node.js/Express

server.js
// server.js
import express from 'express'
import { Tenzro } from '@tenzro/sdk'

const app = express()
const tenzro = new Tenzro({
  apiKey: process.env.TENZRO_API_KEY
})

app.post('/api/generate', async (req, res) => {
  const result = await tenzro.cortex.generate({
    prompt: req.body.prompt,
    model: "gpt-4o"
  })
  res.json({ content: result.content })
})

app.listen(3000)

Advanced Examples

Error Handling & Retries

retry-utils.ts
// utils/retry.ts
export async function withRetry<T>(
  fn: () => Promise<T>,
  maxRetries = 3,
  delay = 1000
): Promise<T> {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await fn()
    } catch (error) {
      if (i === maxRetries - 1) throw error
      await new Promise(resolve => setTimeout(resolve, delay * Math.pow(2, i)))
    }
  }
  throw new Error('Max retries exceeded')
}

// Usage with Tenzro
const generateWithRetry = async (prompt: string) => {
  return withRetry(async () => {
    return tenzro.cortex.generate({
      prompt,
      model: "gpt-4o"
    })
  })
}

Streaming Responses

streaming-chat.tsx
// React streaming component
function StreamingResponse({ prompt }: { prompt: string }) {
  const [content, setContent] = useState('')
  const [isStreaming, setIsStreaming] = useState(false)
  
  const startStream = async () => {
    setIsStreaming(true)
    setContent('')
    
    const stream = await tenzro.cortex.generateStream({
      prompt,
      model: "gpt-4o",
      stream: true
    })
    
    for await (const chunk of stream) {
      if (chunk.content) {
        setContent(prev => prev + chunk.content)
      }
    }
    
    setIsStreaming(false)
  }
  
  return (
    <div>
      <button onClick={startStream} disabled={isStreaming}>
        {isStreaming ? 'Streaming...' : 'Start Generation'}
      </button>
      <div className=<STRING>"mt-4 whitespace-pre-wrap"</STRING>>{content}</div>
    </div>
  )
}

Next Steps

Get Started

  • • Install the SDK: npm install @tenzro/sdk
  • • Get your API key from platform.tenzro.com
  • • Copy examples and replace the API key
  • • Check out the full SDK documentation