Skip to content

Create a generation task

By reading this document, you will learn how to create and generate tasks without using our SDK.

Understand GraphQL

Our API is organized with GraphQL. GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data and it provides a more efficient and developer-friendly way to interact with data.

With GraphQL, you can only focus on the parts that you really need to care about.

API endpoint

The API endpoint is https://api.pixai.art/graphql. You can send a POST request to this endpoint with the GraphQL query.

And we support WebSocket for subscriptions. You can subscribe to the task updates in real time. And the WebSocket endpoint is wss://gw.pixai.art/graphql.

Create a generation task

To create a generation task, you need to access the Mutation.createGenerationTask mutation and pass the parameters. The only parameter you need to pass is “parameters”. Due to the large number of fields supported by this parameter and their frequent changes, we have not defined a GraphQL type for it. Instead, we accept a JSON object. You can refer to the task parameters in the documentation to understand the detailed parameter list.

mutation createGenerationTask($parameters: JSONObject!) {
createGenerationTask(parameters: $parameters) {
id
}
}

Almost all parameters are optional. You can simply fill in a prompts parameter to get started quickly.

{
"parameters": {
"prompts": "1girl"
}
}

Waiting for the task to be completed

After creating a generation task, you can retrieve the task through GraphQL through Query.task.

query getTaskById($id: String!) {
task(id: $id) {
id
status
outputs
}
}

The status field in the Task object is the status of the task. The outputs field is the output of the task. You can check the status field to determine whether the task is completed. If the task is completed, the status field will be completed. You can then access the outputs field to get the output of the task. The simplest way is to wait for the generation to complete by polling the status of the task.

However, you can also use GraphQL subscriptions based on WebSocket.

subscription subscribeMyTaskUpdated {
personalEvents {
taskUpdated {
id
outputs
status
}
}
}

By this way, you can almost perceive the completion in real time.

Next steps

After the task is completed, you can refer to the next document to download the generated images.