Skip to content

Create Simulation

This example demonstrates how to create and run a thermal simulation on an existing G-Code in the Helio Additive platform. Simulations analyze the thermal properties of your print to identify potential quality issues.

Create the following GraphQL files, then run your language’s codegen tool to generate the types.

mutations/CreateSimulation.graphql
mutation CreateSimulation($input: CreateSimulationInput!) {
createSimulation(input: $input) {
id
name
}
}

API Reference:

  1. Setup the client and configuration

    index.ts
    import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client";
    import { CreateSimulationDocument } from "./mutations/__generated/CreateSimulation.generated";
    import { SimulationDocument } from "./queries/__generated/Simulation.generated";
    // Load PAT from environment
    const PAT = process.env.PAT;
    if (!PAT) {
    throw new Error("PAT environment variable is required");
    }
    // Create Apollo Client
    const client = new ApolloClient({
    cache: new InMemoryCache(),
    link: new HttpLink({
    uri: "https://api.helioadditive.com/graphql",
    headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${PAT}`,
    },
    }),
    });
    // Replace with an actual G-Code ID from your account
    const GCODE_ID = process.env.GCODE_ID ?? "your-gcode-id";
  2. Create the simulation

    Create a new simulation for the specified G-Code.

    async function createSimulationEntry(gcodeId: string, name: string) {
    const { data } = await client.mutate({
    mutation: CreateSimulationDocument,
    variables: {
    input: {
    gcodeId: gcodeId,
    name: name,
    },
    },
    });
    const simulationId = data?.createSimulation?.id;
    if (!simulationId) {
    throw new Error("Failed to create simulation");
    }
    return simulationId;
    }
  3. Poll and wait for simulation to complete

    After creation, the simulation needs to run. Poll the status until it’s finished.

    async function waitForSimulationComplete(simulationId: string): Promise<void> {
    return new Promise((resolve, reject) => {
    const checkStatus = async () => {
    const { data } = await client.query({
    query: SimulationDocument,
    variables: { id: simulationId },
    fetchPolicy: "no-cache",
    });
    const status = data?.simulation?.status;
    const progress = data?.simulation?.progress;
    console.log(`Simulation status: ${status}, progress: ${progress}%`);
    if (status === "FINISHED") {
    console.log(`Thermal Index G-Code URL: ${data?.simulation?.thermalIndexGcodeUrl}`);
    resolve();
    } else if (status === "FAILED") {
    reject(new Error("Simulation failed"));
    } else {
    // Poll every 2 seconds
    setTimeout(checkStatus, 2000);
    }
    };
    checkStatus();
    });
    }
  4. Put it all together

    Combine all the steps into a single function and run it.

    async function createSimulation(gcodeId: string, name: string) {
    console.log(`Creating simulation for G-Code: ${gcodeId}`);
    // Create the simulation
    const simulationId = await createSimulationEntry(gcodeId, name);
    console.log(`Simulation created with ID: ${simulationId}`);
    // Wait for simulation to complete
    console.log("Waiting for simulation to complete...");
    await waitForSimulationComplete(simulationId);
    console.log("Simulation finished!");
    return simulationId;
    }
    // Example usage
    const simulationName = `simulation-${Date.now()}`;
    createSimulation(GCODE_ID, simulationName)
    .then((id) => console.log(`Successfully completed simulation: ${id}`))
    .catch(console.error);
  1. Create a .env.local file with your PAT and G-Code ID:

    Terminal window
    PAT="your-personal-access-token"
    GCODE_ID="your-gcode-id"
  2. Update the GCODE_ID with a valid G-Code ID from your account that is in READY status.

  3. Run the example:

    Terminal window
    bun run index.ts