Page 1 of 1

Using REST API with opencart 3.0.3.8. questions

Posted: Mon May 08, 2023 4:58 pm
by sandstorm001
Firstly, I'm really new to this web side coding but thanks to AI, learning is a whole lot easier these days. Just wanted to ask about using the Opencart API connection, is there a limit on what I can request though the API from my store? I tried an example code to list all my products from my store with Python but doesn't really want to play nice:

Code: Select all

import requests
import json

api_url = 'https://yourstore.com/index.php?route=api/products'
api_key = 'your_api_key'

headers = {
    'Accept': 'application/json',
    'X-Oc-Store-Id': '0',
    'X-Oc-Api-Key': api_key
}

response = requests.get(api_url, headers=headers)

if response.status_code == 200:
    products = json.loads(response.text)
    print(products)
else:
    print('Error:', response.status_code, response.text)
Instead I get errors:

Code: Select all

Traceback (most recent call last):
  File "/home/tuomas/apitest.py", line 16, in <module>
    products = json.loads(response.text)
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
My api key is correct, my ip is correct but no connection. I was wondering, does the api url need to be different if I use seo-urls? Or is the ai-generated code just missing something?

Re: Using REST API with opencart 3.0.3.8. questions

Posted: Mon May 08, 2023 6:13 pm
by ADD Creative
Have you read the documentation? https://docs.opencart.com/en-gb/system/users/api/

The API is not a REST API as it needs a session. You have to use api/login to get the api_token and then use the same session, passing the api_token as a URL parameter.

The other data including the key needs to be passed in the POST data, not he header.

There is no route for api/products.

Re: Using REST API with opencart 3.0.3.8. questions

Posted: Sat May 20, 2023 2:55 am
by sandstorm001
Sorry for my late response. I wonder how dropshipping webshops add their products to opencart without doing it manually if there is no api access to products. Modifying database seems a bit extreme.

Re: Using REST API with opencart 3.0.3.8. questions

Posted: Sat May 20, 2023 7:27 am
by ADD Creative
They will be using a custom extension.