Help getting started with the Python API

Samuel_Hurley
Tourist
6 0 2

I'm migrating to Shopify from an existing platform and need to import quite a lot of orders into the accounts of my customers.  As it's not possible to import orders within my store itself, I'm trying to create a script or app that can do this programmatically.  I've already tried creating a script that uses my store's 'Create an order' page, but this is slow and not very reliable.  So I guess I need to create a private app to do this.  According to the API docs I've read, that should be possible.  Please correct me if I'm wrong.

I've created a private app in my admin and installed the Python API (as I'm quite familiar with Python).  Now, I'm trying to follow the instructions in the 'Getting Started' docs on the Python API github (https://github.com/Shopify/shopify_python_api).  

The first two steps go fine, but I cannot get past step 6.  When I try 'shop = shopify.Shop.current()', I get ''ValueError: No shopify session is active".  Why is this?  Am I (or the documentation on github) missing something obvious?  As an aside, the 'API troubleshooting guide' mentioned in the NEW TOPIC notes just brings me to (https://docs.shopify.com/api/introduction/getting-started).  I don't see/find a wiki page as the link suggests I should.

Any insight would be greatly appreciated.

0 Likes
Samuel_Hurley
Tourist
6 0 2

I managed to figure it out and am posting the solution here to benefit others looking for this info.

import shopify

API_KEY = 'app API key'
PASSWORD = 'app password'
SHOP_NAME = 'your shop name'

shop_url = "https://%s:%s@%s.myshopify.com/admin" % (API_KEY, PASSWORD, SHOP_NAME)
shopify.ShopifyResource.set_site(shop_url)
shop = shopify.Shop.current()

order = shopify.Order()
order.email = "foo@foo.com"
order.note = "a test note here"
order.fulfillment_status = "fulfilled"
order.send_receipt = True
order.send_fulfillment_receipt = False
order.line_items = [{"variant_id": 01234567890, "quantity": 1}]

success = order.save()
print success

 

philip_dave
New Member
2 0 1

Hi There,

 

I have tried the above but when I get to the 

shopify.Shop.current()
step it throws a "ValueError: invalid literal for int() with base 10" for the part of the URL that is just before the '@' symbol (the API PASSWORD).
Any suggestions? I am unsure what is the issue with this. 
Tom_Gatenby
Shopify Expert
2 0 0

Hi This is a problem I have come across and I couldn't get it to work. I have tried this so far but I don't think it's gong to work for private apps:

 

shopify.Session.setup(api_key=API_KEY, secret=SHARED_SECRET)

session_url = "%s.myshopify.com" % (SHOP_NAME)
session = shopify.Session(session_url, API_VERSION)

scope=["read_customers"]
permission_url = session.create_permission_url(scope, "https://my_redirect_uri.com")

I still need to provide a valid redirect URL but I couldn't get anywhere with the :

shop_url = "https://%s:%s@%s.myshopify.com/admin/api/%s" % (API_KEY, PASSWORD, SHOP_NAME, API_VERSION)
shopify.ShopifyResource.set_site(shop_url)

0 Likes
philip_dave
New Member
2 0 1

So I have worked past this. This is what I am using for Private apps

shop_url = "https://%s.myshopify.com/admin" % ( self.SHOP_NAME)
 
shopify.ShopifyResource.set_user(self.API_KEY)
shopify.ShopifyResource.set_password(self.PASSWORD)
shopify.ShopifyResource.set_site(shop_url)
self.shop = shopify.Shop.current()
 
So I suspect that the workaround will work for you as well.
0 Likes
yuyu_8
Shopify Partner
5 0 4

Hi there.

 

Need help with this :

when I run this :

shopify.ShopifyResource.set_user(self.API_KEY)

error :

File "<stdin>", line 1, in <module>
NameError: name 'shopify' is not defined

 

may i know what is the problem? 

 

I followed https://github.com/Shopify/shopify_python_api and stuck at no 2.

Can someone explains to me more about this? I'm not that familiar with Shopify API and the python coding

 

Thanks in advance.

0 Likes
xxyb720
New Member
1 0 0

I think you just need to import shopify

0 Likes
yuyu_8
Shopify Partner
5 0 4

Hi there,

 

It would be a great help if you explain more to me. I am quite new to the Shopify environment.

 

Thanks in advance.

 

Cheers,

Yuyu

0 Likes
Vinh
Tourist
8 0 2

You have to import the library to be able to use it. So first you can pip install ShopifyAPI in a command line, like the cmd in Windows

pip install --upgrade ShopifyAPI

Then in your python you execute the import

import shopify
After that, your code will work
Cvanderlick
New Member
1 0 0

Its being you did not import shopify.

Add this line below to the beginning of your file on line 1 and move everything else below.

import shopify

0 Likes