Skip to main content

Calling AI APIs

Most AI generation APIs follow a similar pattern. To create a new job, you call a specific mutation. For example:

mutation {
generateText(input: { message: "Generate a poem about cats" }) {
response {
text
}
}
}

Calling this endpoint will create a generation job and return the full response in the same call.

You can also "fire and forget" the mutation and later retrieve its result via a query:

mutation GenerateText {
generateText(input: { message: "Generate a poem about cats" }) {
id
nonce
}
}

query GetTextGenerationResult($id: ID!, $nonce: String!) {
generateTextResults(id: $id, nonce: $nonce) {
response {
text
}
}
}

Some APIs include a streaming API that allows you to subscribe to the response as it is being generated:

mutation StartConversation($input: AIConversationInput!) {
conversation(input: $input) {
id
nonce
}
}

subscription StreamConversation($id: ID!, $nonce: String!) {
conversationStream(id: $id, nonce: $nonce) {
complete
text
}
}