package lib import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" ) const printful_api_url = "https://api.printful.com" type Printful_Order_Item struct { Variant_ID int `json:"variant_id"` Quantity int `json:"quantity"` } type Printful_Order_Request struct { Recipient struct { Name string `json:"name"` Address string `json:"address1"` City string `json:"city"` State string `json:"state_code"` Country string `json:"country_code"` Zip string `json:"zip"` } `json:"recipient"` Items []Printful_Order_Item `json:"items"` } type Printful_Order_Response struct { Code int `json:"code"` Result struct { ID int `json:"id"` } `json:"result"` } func Create_Printful_Order(order_request Printful_Order_Request) (*Printful_Order_Response, error) { api_key := os.Getenv("PRINTFUL_API_KEY") if api_key == "" { return nil, fmt.Errorf("PRINTFUL_API_KEY not set") } body, err := json.Marshal(order_request) if err != nil { return nil, err } req, err := http.NewRequest("POST", printful_api_url+"/orders", bytes.NewBuffer(body)) if err != nil { return nil, err } req.Header.Set("Authorization", "Bearer "+api_key) req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() response_body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } var printful_response Printful_Order_Response if err := json.Unmarshal(response_body, &printful_response); err != nil { return nil, err } if printful_response.Code != 200 { return nil, fmt.Errorf("printful API error: code %d", printful_response.Code) } return &printful_response, nil }