Keep your AI code: OpenAI SDK, Vercel AI SDK, LangChain
ChargeMyAI is the sign-in and payments layer; your prompts, tools and agents stay yours.
ChargeMyAI speaks OpenAI's chat API. Point the SDK you already use at it and pass charge.fetch. That's the whole change: sign-in, the user's consent and UPI top-ups happen in ChargeMyAI's sheets, the way an auth provider handles login, and your code keeps its tools, JSON output, streaming and agent loops.
index.html
<script src="https://api.chargemyai.com/v1/charge.js"></script>
openai.js
import OpenAI from 'openai'
const ai = new OpenAI({
baseURL: charge.baseURL, // https://api.chargemyai.com/v1
apiKey: 'chargemyai', // charge.fetch puts the user's own token here
fetch: charge.fetch,
dangerouslyAllowBrowser: true, // safe: the token is the user's, capped and only works on your site
})
const res = await ai.chat.completions.create({
model: 'standard', // or 'premium', or an id from https://api.chargemyai.com/v1/models
messages: [{ role: 'user', content: 'Plan a 3-day Ooty trip under ₹15,000' }],
tools, // function tools run in your code, as usual
}) vercel-ai.js
import { createOpenAICompatible } from '@ai-sdk/openai-compatible'
import { generateObject } from 'ai'
import { z } from 'zod'
const chargemyai = createOpenAICompatible({ name: 'chargemyai', baseURL: charge.baseURL, fetch: charge.fetch, supportsStructuredOutputs: true })
const { object } = await generateObject({
model: chargemyai('standard'),
schema: z.object({ total: z.number(), due: z.string() }),
prompt: 'Pull the total and due date from: ' + billText,
}) langchain.js
import { ChatOpenAI } from '@langchain/openai'
const llm = new ChatOpenAI({ model: 'standard', apiKey: 'chargemyai', configuration: { baseURL: charge.baseURL, fetch: charge.fetch } }) What charge.fetch does
- Adds the user's token to calls to ChargeMyAI, and never to any other address.
- Not signed in, out of balance, or the model runs outside India: it shows the right ChargeMyAI sheet, then sends the same call again. Your code just gets the answer.
- Anything the user has to fix themselves (their daily limit, paused spending) or a sheet they close comes back as the original error, so your SDK raises it as usual. Show
error.message.
Good to know
- Every call is billed to the user by tokens, held up front at the most it can cost and settled to what was used. Non-streamed replies include
chargemyai.cost_paiseandbalance_paise. - Function tools,
response_format, streaming, images (data or https URLs) and PDFs (fileparts) work as in OpenAI's API. Web search, more than one choice (n), and audio are refused, since they cost outside tokens. - Not using
charge.fetch? Get the token withawait charge.connect(), and pass errors toawait charge.resolve(error): it shows the sheet and returnstruewhen you should send the call again.
Read next
Still stuck? Talk to us and we'll help.