Search code examples
pythonjsonapipython-requests

Create product in printful using API - Python


I am trying to a create a product using the Printful api and python. I was able to create a product on there website (a basic) hoodie, and I wanted to try and create the same hoddie using python.

Here is the documentation I am using: https://developers.printful.com/docs/#operation/createSyncProduct

I keep getting this error which is very generalistic:

{
    "code": 400,
    "result": "This API endpoint applies only to Printful stores based on the Manual Order / API platform. Find out more at Printful API docs.",
    "error": {
        "reason": "BadRequest",
        "message": "This API endpoint applies only to Printful stores based on the Manual Order / API platform. Find out more at Printful API docs."
    }
}

here is the code I am trying for testing:

import requests
import json

my_key = "my_token"

headers = {
    "Authorization" : "Bearer "+my_key
}


data = {
            "id": 302079884,
            "external_id": "8136758395169",
            "name": "Unisex Hoodie",
            # "variants": 6,
            "thumbnail_url": "https://cdn.shopify.com/s/files/1/0713/4275/2033/products/unisex-premium-hoodie-black-front-64079acdc1ff0_grande.jpg?v=1678219998",
            "is_ignored": False
        }
data = json.dumps(data)

products = requests.post("https://api.printful.com/store/products",headers=headers,data=data)
print(json.dumps(products.json(),indent=4))

I am not sure how to test this code because the response is very generalistic.

I was able to test my token to make sure it works by trying to get the product I made on the website:

import requests
import json

my_key = "my_token"

headers = {
    "Authorization" : "Bearer "+my_key
}

products = requests.get("https://api.printful.com/sync/products",headers=headers)
print(json.dumps(products.json(),indent=4))

and I got the response:

{
    "code": 200,
    "result": [
        {
            "id": 302079884,
            "external_id": "8136758395169",
            "name": "Unisex Hoodie",
            "variants": 6,
            "synced": 6,
            "thumbnail_url": "https://cdn.shopify.com/s/files/1/0713/4275/2033/products/unisex-premium-hoodie-black-front-64079acdc1ff0_grande.jpg?v=1678219998",
            "is_ignored": false
        }
    ],
    "extra": [],
    "paging": {
        "total": 1,
        "offset": 0,
        "limit": 20
    }
}

So I know I am hitting the api correctly. If anyone has any suggestions on how to create a basic product using the printful api, that would be awesome.

EDIT: I also tried what @Driftr95 mentioned regarding wrapping the data around "sync_product" but that still didn't work. Here is the new data variable now:

data = {
    "sync_product": {
        "name": "API product Bella",
        "thumbnail": "https://imagemagick.org/image/wizard.jpg"
    },
    "sync_variants": [
        {
            "retail_price": "21.00",
            "variant_id": 44666144522529,
            "files": [
                {
                    "url": "https://imagemagick.org/image/wizard.jpg"
                },
                {
                    "type": "back",
                    "url": "https://imagemagick.org/image/wizard.jpg"
                }
            ]
        }
    ]
}

but I still get:

{
    "code": 400,
    "result": "This API endpoint applies only to Printful stores based on the Manual Order / API platform. Find out more at Printful API docs.",
    "error": {
        "reason": "BadRequest",
        "message": "This API endpoint applies only to Printful stores based on the Manual Order / API platform. Find out more at Printful API docs."
    }
}

That response is so vague it is very hard to tell what is wrong with the post data I am trying to send.


Solution

  • I figure out the answer on my own. For those running across this issue in the future I figured it would be best to show the code.

    First out the issue here:

    { "code": 400, "result": "This API endpoint applies only to Printful stores based on the Manual Order / API platform. Find out more at Printful API docs.", "error": { "reason": "BadRequest", "message": "This API endpoint applies only to Printful stores based on the Manual Order / API platform. Find out more at Printful API docs." } }

    Is because I was using an API token/key that was directly for shopify. When you create an API key/token for printful, it will ask you what store you want it to apply to. Since I ONLY had a test shopify store at the time, I decided to use that token, but the endpoint I was using doesn't allow you to create products directly from printful into shopify or your ecommerce store. So I created a test store with printful only and used an API key/token that worked for that directly.

    I then ran across another issue because I didn't specifiy colors of the hoodie I was trying to create. So I decided to try a mockup which I assume is the most basic thing you can make.

    Here is what fixed the code. Try this sample data and this should work:

    data = {
        "sync_product": {
                "name": "Test Product",
            },
            "sync_variants": [
                {
                    "external_id": "44666144522529",
                    "variant_id": 10779,
                    "retail_price": "31.00",
                    "sku": "7563051_10779",
                    "files": [
                        {
                            "type": "mockup",
                            "url": None,
                        }
                    ]
                }
            ]
    }
    

    from here you can slowly tweak what you need based off the products you can use in printful's catalog.