Skip to main content

What You’ll Build

An AI writing assistant: users enter a topic, and AI generates a draft article. Each call is billed based on actual token consumption — you just need an API Key, and the Profy SDK handles model calls and usage tracking. The end result:
  • Use the Profy SDK’s chat.completions.create to call AI models
  • Support both streaming and non-streaming modes
  • SDK automatically handles SSE parsing, error mapping, and timeouts
  • Token usage is returned in responses; the platform tracks it automatically

Calling Method Comparison

This tutorial uses the Profy SDK’s chat.completions. If your scenario involves invoking published Experts (with persona + tools + memory), see Expert Invocation.

Prerequisites

  1. Have a Profy developer account
  2. Created an API Key (sk-pro- prefix) in the Platform Console
  3. Installed the SDK: npm install @profy-ai/sdk or pip install profy-sdk

Step 1: Initialize the Client

The API Key is a server-side credential — do not expose it in frontend code or Git repositories. Inject it via environment variables.

Step 2: Non-Streaming Call

Key fields in ChatResponse:

Step 3: Streaming Call

Set stream: true for real-time streaming output, ideal for typewriter-style chat interfaces.

Step 4: Error Handling

The SDK throws typed exceptions based on HTTP status codes:
Do not retry on InsufficientBalanceError (402) — insufficient balance won’t change with retries. Show a top-up prompt instead.

Step 5: Query Available Models

The available model list depends on the platform administrator’s configuration. Common models include deepseek-chat, deepseek-reasoner, qwen-plus, etc.

Complete Example

An AI writing assistant backend with streaming and error handling.

OpenAI SDK Compatibility (Alternative)

Profy’s /v1/chat/completions endpoint is OpenAI-compatible, so you can use the official OpenAI SDK directly:
We recommend using the Profy SDK — it provides typed exceptions, Expert invocation, and other Profy-specific features. The OpenAI SDK approach is ideal for projects migrating from OpenAI.

Next Steps

Per-Use Billing

Implement per-call billing with ProfyApp reportEvent

Expert Invocation

Invoke published AI Experts on the platform

Next.js Full-Stack Integration

Complete OAuth + billing + AI call full-stack application

SDK Quick Start

Install SDK, create API Key, first call