The sample code below reads an OBJ file and passes it to our API telling the API to convert it to STL format.
The sample below assumes you have exported your API token as an environment variable like so.
export ZOO_API_TOKEN="YOUR-API-TOKEN" Replace YOUR-API-TOKEN with one of your API tokens. It looks like you need to generate an API Token first.
Below is our API-client code:
#!/usr/bin/env python3 from typing import Dict from kittycad import KittyCAD, KittyCADAPIError from kittycad.models.base64data import Base64Data from kittycad.models.file_conversion import FileConversion from kittycad.models.file_export_format import FileExportFormat from kittycad.models.file_import_format import FileImportFormat from kittycad.types import Unset # Create a new client with your token parsed from the environment variable: # ZOO_API_TOKEN. client = KittyCAD(timeout=500, verify_ssl=True) # Convert a file from OBJ to STL. # Read in the contents of the file. file = open("./ORIGINALVOXEL-3.obj", "rb") # LITTERBOX-END-NON-EDITABLE-SECTION content = file.read() file.close() try: result = client.file.create_file_conversion( body=content, src_format=FileImportFormat.OBJ, output_format=FileExportFormat.STL, ) except KittyCADAPIError as e: raise Exception(f"There was a problem: {e}") fc: FileConversion = result print(f"File conversion id: {fc.id}") print(f"File conversion status: {fc.status}") if isinstance(fc.outputs, Unset): raise Exception("Expected outputs to be set") if fc.outputs is None: raise Exception("Expected outputs to be set") outputs: Dict[str, Base64Data] = fc.outputs if len(outputs) != 1: raise Exception("Expected one output file") # LITTERBOX-START-NON-EDITABLE-SECTION for _, output in outputs.items(): output_file_path = "./output.stl" print(f"Saving output to {output_file_path}") output_file = open(output_file_path, "wb") output_file.write(output) output_file.close() package main import ( "fmt" "os" "github.com/kittycad/kittycad.go" ) func main() { // Create a new client with your token parsed from the environment variable: // ZOO_API_TOKEN. client, err := kittycad.NewClientFromEnv("your apps user agent") if err != nil { panic(err) } fileBytes, _ := os.ReadFile("./ORIGINALVOXEL-3.obj") // LITTERBOX-END-NON-EDITABLE-SECTION fc, err := client.File.CreateConversion( kittycad.FileExportFormatStl, kittycad.FileImportFormatObj, fileBytes, ) if err != nil { panic(err) } fmt.Println("File conversion id: ", fc.ID) fmt.Println("File conversion status: ", fc.Status) // LITTERBOX-START-NON-EDITABLE-SECTION output_file_path := "./output.stl" fmt.Println("Saving output to ", output_file_path) output, err := os.Create(output_file_path) if err != nil { panic(err) } defer output.Close() if len(fc.Outputs) != 1 { panic("Expected 1 output file") } for _, contents := range fc.Outputs { if _, err := output.Write(contents.Inner); err != nil { panic(err) } } if err := output.Sync(); err != nil { panic(err) } }