[Python] Create product in printful using API - Python

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página