Skip to content

AI in your application

Markdown

Your application can ask a model to read text, PDFs and images and to answer in text, or in JSON that follows a schema you give. Apigami calls the model in its own name, in the EU: you need no AI account, no key and no server. What you send is processed without being stored and is never used for training.

POST /call
{
"op": "ai.generate",
"params": {
"instructions": "Extract the person's name and city. Use null for what the text does not say.",
"input": [{ "text": "Jean Dupont, developer, lives in Lyon." }],
"output_schema": {
"type": "object",
"properties": {
"name": { "type": "string" },
"city": { "type": ["string", "null"] }
},
"required": ["name", "city"]
}
}
}
Result
{
"result": {
"output": { "name": "Jean Dupont", "city": "Lyon" },
"finish_reason": "stop",
"usage": { "input_tokens": 188, "output_tokens": 43 }
}
}

ai.generate is answered by Apigami itself, never by a browser: it takes no domain and no user_ref. A run that serves a person needs that person’s grant of your AI, asked like a site’s; a run that serves nobody needs only the declaration.

The AI is in preview, open to a few teams. Any other team’s call answers 403 ai is not open to this group yet.

  1. Declare the permission in your manifest, beside your sites (Permissions: ai):

    "x-apigami": { "permissions": ["ai"] }

    The version whose code calls must declare it: the one a run executes, the production version for your apg_ key and for APIGAMI_KEY. For a sub-app, the version the calling application reaches must declare it too, since that is what the connect page of the applications using it shows.

  2. Deploy, and publish to call from your server with your API key. The connect page then lists, among what your application may do, « Use an AI: what you entrust to it is processed in the cloud, without storage or training », and « Continue » grants it.

  3. The people connected before a version that declares ai are asked on Apigami’s screen of new permissions at their next opening of one of your pages. Until they answer, a run for them answers 403 consent requested with "permission": "ai", and after a refusal 403 no grant. A cron, an open action or your API key, which serve nobody, are not concerned. A member of your team trying a version with app_run or app_page_preview has its AI granted for that trial.

Caller Credential
Your server your API key, apg_…
The action of a cron, for several people env.APIGAMI_KEY, the production version’s key
An action, a trigger, a schedule X-Apigami-Run, what Apigami handed the action
An open action env.APIGAMI_KEY: it gets no X-Apigami-Run

The AI answers your application, not a person: a call with nobody behind it, such as an open action or a schedule, calls it like any other. A page cannot: its token is the visitor’s, who could write the instructions. Call an action of yours from the page with apigami.call, and call the AI from that action.

actions/extract.ts
export const extractPerson = async (env, runToken: string, pdfBase64: string) => {
const answer = await fetch(`${env.APIGAMI_API_URL}/call`, {
method: 'POST',
headers: { 'X-Apigami-Run': runToken, 'Content-Type': 'application/json' },
body: JSON.stringify({
op: 'ai.generate',
params: {
instructions: 'Extract the person this resume is about.',
input: [{ file: { media_type: 'application/pdf', data_base64: pdfBase64 } }],
output_schema: { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] },
},
}),
})
const body = await answer.json()
// Never log the input or the output: your logs are kept, and the content is someone else's.
if (!answer.ok) throw new Error(`ai refused: ${answer.status} ${body.error}`)
return body.result.output
}
Param
instructions required What the model must do, 1 to 32 000 characters
input required 1 to 10 parts, read in order: { "text": "…" } or { "file": { "media_type": "…", "data_base64": "…" } }
output_schema optional A JSON Schema of the answer, 16 kB at most. Absent, the answer is text
max_output_tokens optional 1 to 16 384, 4 096 by default
model optional gemini-3.1-flash-lite by default, claude-haiku-5-5 or claude-sonnet-5-5

The model’s settings are Apigami’s: they may change, never the contract.

Without model, gemini-3.1-flash-lite answers. claude-haiku-5-5 and claude-sonnet-5-5 are in trial: they may answer 503 ai is not configured while they are not open. They take the same parameters, with three limits:

  • an image is 5 MB at most, decoded, each one; a larger one is refused with its path;
  • output_schema uses fewer keywords (Structured output);
  • a request the model chooses not to answer is 422 the model declined to answer: refusal.
  • Text parts total 500 000 characters at most.
  • A file is a PDF (application/pdf), plain text (text/plain) or an image (image/png, image/jpeg, image/webp), in standard base64. Its first bytes must match its type.
  • 14 MB of files at most per call, decoded, all files together.
  • A Word or OpenDocument file is refused with a 415: convert it to PDF or text first.

With output_schema, output is the parsed JSON, which follows the schema. A schema written by zod’s z.toJSONSchema or pydantic passes as it is. The keywords the model reads are type, properties, required, items, prefixItems, additionalProperties, enum (strings or numbers), anyOf, oneOf, title, description, format, minItems, maxItems, minimum, maximum, propertyOrdering, $schema, $id, $anchor, $defs and $ref. Any other, such as pattern or minLength, is refused with its path rather than silently ignored. A value that may be absent is written "type": ["string", "null"].

With a Claude model, the keywords are type, properties, required, items, additionalProperties, enum, anyOf, title, description, format, minItems (0 or 1 only), $schema, $id, $defs and $ref, a $ref pointing into the schema such as #/$defs/job. Every object is closed: additionalProperties may only be false, and Apigami writes it on each object that leaves it out.

description is read by the model: say there what a field holds, and what to answer when the document does not say.

usage counts the tokens of the call. output_tokens includes the model’s reasoning. Without a schema, finish_reason is max_output_tokens when the text was cut short; with a schema, a cut answer is a 422, since half a JSON is of no use.

Status error What to do
400 ai answers the caller, not a person: remove user_ref and install_ref Send neither
403 a page cannot call the ai: … Call it from an action, which the page calls with apigami.call
403 not_a_subapp: … A sub-app’s run, after the application calling it stopped declaring it
403 subapp_not_accepted A sub-app’s run, after the person removed that optional sub-app
409 publish a version with app_publish before an api key calls the ai: … Publish once: your key runs production’s manifest
403 ai not permitted for this app: … Declare "permissions": ["ai"] in the version that calls, and deploy
403 ai is not open to this group yet The AI is in preview: write to contact@apigami.com
403 consent requested, with "permission": "ai" The person never decided your AI: send them to one of your pages, where Apigami asks them. With consentPending, the approval window is on screen
403 no grant, with "permission": "ai" The person refused your AI; send them to settings_url (Refusals)
400 invalid ai params: params.input[1].file.data_base64: does not start like application/pdf Fix the field it names
400 invalid ai params: params.model: must be one of … Name one of the models, or leave model out
413 ai input too large: at most 14 MB of files per call Send fewer or smaller files
415 unsupported media type …: send pdf, text or an image Convert the file to PDF or text
400 invalid ai params: the model cannot read this input, … Check the files, and the schema
503 ai is not configured Apigami’s side: write to Apigami
503 the model is busy, try again in a minute Wait for Retry-After, then try again
502 the model could not answer, try again later Try again later
422 the model declined to answer: safety The content was refused by the model’s filters, refusal for a Claude model: do not retry as is
422 ai output cut at max_output_tokens: … Raise max_output_tokens, or ask for less

A refusal never repeats what you sent. Apigami never retries a call by itself: a call is billed once.

ai.transcribe turns a recording into text, in the language it detects. It is called like ai.generate: the same credentials, the same ai permission and the same person’s grant, no domain, no user_ref, never from a page. It is in trial, open to Jarvi’s team only, on Jarvi’s own transcription key: any other team’s call answers 403 ai is not open to this group yet, even one open to ai.generate.

POST /call
{
"op": "ai.transcribe",
"params": {
"file": { "mime_type": "audio/webm", "data": "GkXfo59ChoEBQveBAULygQRC84EIQoKE…" },
"languages": ["fr", "en"]
}
}
Result
{
"result": {
"text": "Bonjour, je vous appelle au sujet de la candidature de Jean.",
"language": "fr",
"audio_duration_seconds": 6.4
}
}
Param
file.mime_type required audio/webm, audio/ogg, audio/mp4, audio/mpeg, audio/wav or audio/x-m4a. A parameter such as ;codecs=opus, which MediaRecorder writes, is ignored
file.data required The audio in standard base64, 14 MB at most once decoded
languages optional The languages the audio may be in, as codes such as fr or en_us (EN-US is read as en_us), 1 to 20. By default fr, en, es, nl, it, de and pt. Narrowing it keeps a muffled recording from being read as another language
  • language is the code detected among languages, null when none was given back; audio_duration_seconds is the recording’s length as the transcription measured it, or null.
  • A transcription takes a few seconds for a voice note and is given 120 s; the call answers within 130 s at most. Past the 120 s, the call is a 502: send shorter recordings, a few minutes each.
  • text is empty when the recording holds no speech.

The refusals of ai.generate that do not depend on its parameters apply as they are: ai not permitted for this app, ai is not open to this group yet, consent requested and no grant with "permission": "ai", ai is not configured, a user_ref or a page’s token. Its own:

Status error What to do
400 invalid ai params: params.file.mime_type: required Fix the field it names
400 invalid ai params: params.languages[0]: must be an ISO 639-1 code such as fr, … Send codes, not names
400 invalid ai params: params.file: the audio cannot be transcribed, … The file holds no audio it can decode: check its content and mime_type
400 invalid ai params: params.languages: a language the transcription does not know, … Send only languages it transcribes, or leave languages out
413 ai input too large: at most 14 MB of audio per call Send a shorter or more compressed recording
415 unsupported media type …: send webm, ogg, mp4, mpeg, wav or m4a audio Convert the recording
502 the transcription took longer than 120 s, … Try again later, or send a shorter recording
502 the model could not answer, try again later Try again later
  • Apigami keeps one line per call: the application, its version, the model, the tokens (for a transcription, the audio’s duration), the duration and whether it answered. Never the instructions, the files, the audio nor the answer.
  • The model’s provider, Google Cloud, processes the call in the EU and stores nothing of it, with one exception: a request its abuse filters flag may be kept up to 90 days, in the EU, for review. Nothing is used for training.
  • The transcription’s provider, AssemblyAI, processes the audio in the EU (api.eu.assemblyai.com). Apigami deletes the transcript, and the audio with it, at AssemblyAI once the transcription has ended, whether it gave a text or failed; one still running when the call times out is deleted when it ends. One exception: an audio uploaded whose transcription never started, because AssemblyAI refused or did not answer the request to start it, has no transcript to delete it with, and stays at AssemblyAI until it expires there.
  • Your application is responsible for what it does with the answers. Never write an input or an output of the AI to your logs: they are kept.