Skip to content

Instantly share code, notes, and snippets.

@bgraves-com
Last active December 3, 2022 16:48
Show Gist options
  • Save bgraves-com/63465a5fcc7d0bd3b01237f358b1c786 to your computer and use it in GitHub Desktop.
Save bgraves-com/63465a5fcc7d0bd3b01237f358b1c786 to your computer and use it in GitHub Desktop.
getting-started-with-the-shopify-api-using-python.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMWrTkfN8eJ36S6Q2AZIwks",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/bgraves-com/63465a5fcc7d0bd3b01237f358b1c786/getting-started-with-the-shopify-api-using-python.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"## Step 1) Install the official Python Shopify API package"
],
"metadata": {
"id": "qbjjH3ZPGKa1"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "x4gtvBQwCgG9"
},
"outputs": [],
"source": [
"!pip install ShopifyAPI"
]
},
{
"cell_type": "code",
"source": [
"import shopify"
],
"metadata": {
"id": "a_9CQmiBItJ6"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"shop_url = \"newpredictiondev.myshopify.com\"\n",
"admin_api_key = 'your_secret_admin_api_key'\n",
"api_version = '2022-07'\n"
],
"metadata": {
"id": "a4d3LnO-HAyg"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"session = shopify.Session(shop_url, api_version, admin_api_key)\n",
"shopify.ShopifyResource.activate_session(session)"
],
"metadata": {
"id": "qM1Zog96EMEC"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"product = shopify.Product.find(8017472717077)\n",
"print(f'The title of this product is \"{product.title}\".')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6VDAiVboL11e",
"outputId": "51a37bdb-d38e-44be-a6ff-4f925996e223"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The title of this product is \"Amazing Ice Sunglasses\".\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Step 2: Adding a new product"
],
"metadata": {
"id": "eMmmPIDevaw7"
}
},
{
"cell_type": "code",
"source": [
"new_product = shopify.Product() # Create a new product Object\n",
"new_product.title = 'Hiking Club Crewneck' # Set the title\n",
"new_product.save() # Save to Shopify"
],
"metadata": {
"id": "39aH0gbySaOL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"print(f'New product details: id={new_product.id}, title={new_product.title}.')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "c9EJtR03UH6M",
"outputId": "2efa1d4a-d960-4a48-d80d-3007ba3acca0"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"New product details: id=8024192516373, title=Hiking Club Crewneck.\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Step 3: Updating existing products"
],
"metadata": {
"id": "qwf9DpdZviBW"
}
},
{
"cell_type": "code",
"source": [
"crewneck = shopify.Product.find(8024192516373)\n",
"print(f'Crewneck details: id={crewneck.id}, title={crewneck.title}.')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VK65ud9ZVqLj",
"outputId": "9252bd88-ef10-4a0f-f29c-151fbba47029"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Crewneck details: id=8024192516373, title=Hiking Club Crewneck.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"crewneck.body_html = \"A very special crewneck covered in patches\"\n",
"crewneck.save()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ji9iXCh5WYAm",
"outputId": "a3f69dfe-5532-40fe-da9f-518e3e618cb5"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 89
}
]
},
{
"cell_type": "code",
"source": [
"print(f'Crewneck details: id={crewneck.id}, title={crewneck.title}, body_html={crewneck.body_html}.')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cQZzEBDZWfTE",
"outputId": "9cbd59e9-eab0-42c3-bfca-72b50ca83c74"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Crewneck details: id=8024240521493, title=Hiking Club Crewneck, body_html=A very special crewneck covered in patches.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"crewneck.attributes\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9NGI1tLBZFAG",
"outputId": "2ab476e9-9d4a-4830-86ff-7e07f08e7e28"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'id': 8024240521493,\n",
" 'title': 'Hiking Club Crewneck',\n",
" 'body_html': 'A very special crewneck covered in patches',\n",
" 'vendor': 'NewPredictionDev',\n",
" 'product_type': '',\n",
" 'created_at': '2022-12-03T10:41:57-05:00',\n",
" 'handle': 'hiking-club-crewneck-1',\n",
" 'updated_at': '2022-12-03T11:05:31-05:00',\n",
" 'published_at': '2022-12-03T10:41:57-05:00',\n",
" 'template_suffix': None,\n",
" 'status': 'active',\n",
" 'published_scope': 'web',\n",
" 'tags': '',\n",
" 'admin_graphql_api_id': 'gid://shopify/Product/8024240521493',\n",
" 'variants': [variant(43961396003093)],\n",
" 'options': [option(10195042074901)],\n",
" 'images': [],\n",
" 'image': None}"
]
},
"metadata": {},
"execution_count": 91
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment