Re: Access to Shopify API from External Web App

Access to Shopify API from External Web App

glai
Shopify Partner
1 0 0

This question might have been asked before, but I'm finding it difficult to glue the pieces of information together in the shopify docs.

 

I'm building a marketplace for businesses, and was hoping to allow my sellers to connect their Shopify stores / upload their products to my marketplace. I'm not looking to build an app inside of Shopify. From the document that I've been reading, it's all referring to building an app inside of Shopify.

 

Is it possible to access store information and seller product information via API to an external marketplace app?

Replies 2 (2)

Liam
Community Manager
3106 339 870

Hi Glai,

 

To send store information and seller product information via API you would need to create a custom app, where you'll be able to get API keys and access to making queries that you'd require.

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Jclewis1989
Shopify Partner
18 1 8

Hey there Glai! I know this post is older, so you very well may have found a solution to this inquiry. In my experience, to access data from a specific instance within Shopify, please find Python / Flask example below for reference:

 

 

@Anonymous_bp.route('/')
def index():
    return render_template('index.html')

@products_bp.route('/products', methods=['GET'])
def get_all_products():
    # Define the URL for the REST API request
    url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products.json'

    # Headers for the REST API request
    headers = {
        'Content-Type': 'application/json',
        'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
    }

    # Make the REST API request
    response = requests.get(url, headers=headers)

    # Check if the request was successful
    if response.status_code == 200:
        return jsonify(response.json())
    else:
        return jsonify({'error': 'Unable to fetch products'}), response.status_code

@products_bp.route('/products/<product_id>', methods=['GET'])
def get_product_by_id(product_id):
    try:
        # Define the URL for the REST API request
        url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products/{product_id}.json'

        # Headers for the REST API request
        headers = {
            'Content-Type': 'application/json',
            'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
        }

        # Make the REST API request
        response = requests.get(url, headers=headers)

        # Check if the request was successful
        if response.status_code == 200:
            return jsonify(response.json())
        elif response.status_code == 404:
            return jsonify({'error': 'Product not found'}), 404
        else:
            return jsonify({'error': 'Unable to fetch product'}), response.status_code
    except Exception as e:
        return jsonify({'error': 'An error occurred', 'message': str(e)}), 500

 

 

Here is the breakdown:

  1. @Anonymous_bp.route('/'):

    • This is a decorator for the root route of the /products Blueprint.
    • When a user visits the root URL of the application (e.g., http://example.com/), the index function is called.
    • index returns an HTML template named 'index.html' using render_template. This template could be a landing page or any other content you want to display when users visit the root URL.
  2. @Anonymous_bp.route('/products', methods=['GET']):

    • This is a decorator for the /products route with an HTTP GET method.
    • When a user visits the '/products' URL (e.g., http://example.com/products), the get_all_products function is called.
    • get_all_products constructs a URL to make a GET request to the Shopify store's API to retrieve all products. It uses the requests library to make the API call.
    • The response is checked for success (HTTP status code 200). If successful, it returns the JSON response with all the products using jsonify. If there's an error, it returns a JSON error message along with the HTTP status code (e.g., 404 for "Product not found" or other status codes for various errors).
  3. @Anonymous_bp.route('/products/<product_id>', methods=['GET']):

    • This is a decorator for the '/products/<product_id>' route with an HTTP GET method. It includes a dynamic parameter <product_id>.
    • When a user visits a URL like '/products/123' (where '123' is a product ID), the get_product_by_id function is called with '123' as the product_id parameter.
    • get_product_by_id constructs a URL to make a GET request to the Shopify store's API to retrieve a specific product based on the provided product_id.
    • Like get_all_products, it checks the response for success and handles different cases: if the product is found (HTTP status code 200), if the product is not found (HTTP status code 404), or if there's an error (other status codes). It returns the appropriate JSON response or error message.

Overall, this code defines three routes:

  1. The root route that displays an 'index.html' template.
  2. A route to retrieve and display all products from the Shopify store's API.
  3. A route to retrieve and display a specific product based on its ID from the Shopify store's API.

Hopefully this is of some guidance / assistance. Curious what you find, let me know!

James Lewis