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 from the user’s credits — you don’t need to manage API keys or build a billing system; the Profy platform handles everything. The end result:
  • Users authorize your App via OAuth
  • Your App calls Profy’s OpenAI-compatible endpoint to generate content
  • Profy automatically tracks token usage and deducts from the user’s credits
  • You (the creator) earn diamond revenue based on your revenue share ratio

METERED vs PER_USE

This tutorial uses METERED mode. If your scenario involves fixed-price single operations, refer to the reportEvent() usage in the SDK Quick Start.

Prerequisites

  1. Have a Profy developer account with approved creator verification
  2. Created an App in Studio with billing type set to METERED
  3. Obtained the App’s clientId and clientSecret
  4. Configured an OAuth callback URL

Step 1: OAuth Authorization

Obtain the user’s Access Token via OAuth. All subsequent AI calls are authenticated and billed through this Token.
For the complete OAuth flow (authorization page redirect, callback handling, Token storage), refer to the SDK Quick Start.

Step 2: Call AI Models (Non-Streaming)

With the Access Token in hand, call Profy’s OpenAI-compatible endpoint directly. The request format is identical to OpenAI’s /v1/chat/completions.
The Access Token is a user-level credential — Profy identifies the user through this Token and deducts from their credits. Never mix Tokens between different users.

Step 3: Streaming Responses

Set stream: true to receive SSE streaming responses, ideal for displaying AI output in real-time.

Step 4: OpenAI SDK Integration

Profy’s chat endpoint is OpenAI-compatible, so you can use the official OpenAI SDK directly — just change the baseURL and apiKey.
Using the OpenAI SDK gives you well-defined types, automatic retries, and robust streaming support. This approach is recommended for production environments.

Step 5: Automatic Token Expiry Handling

Access Tokens are valid for 1 hour. Wrap calls in an auto-refreshing function to avoid manual checks every time.

Step 6: Error Handling and Retries

Do not retry on 402 — the user’s insufficient balance won’t change with retries. Show a top-up prompt instead.

Available Models

The Profy platform administrator configures which models are available. The models your App can use depend on the platform configuration. How to check:
  • API: GET /openapi/v1/meters returns the currently available Meter configuration
  • Studio: Check the “Billing Configuration” in your App settings page
Common models include deepseek-chat, deepseek-reasoner, qwen-plus, and more. The specific list of available models depends on the platform configuration.

Complete Example

An AI writing assistant backend integrating OAuth, Token refresh, streaming output, and error handling.

Next Steps

PER_USE Billing Tutorial

Fixed-price per-use billing for deterministic operations like report exports

Expert Invocation

Invoke published AI Experts on the platform and receive SSE streaming responses

Events API Reference

Complete field documentation for the AI model call endpoint

Publish to Marketplace

After development, submit your App to the Profy Marketplace