Created
June 22, 2021 00:16
-
-
Save michoelchaikin/100a569343a013c7181800f5325c5501 to your computer and use it in GitHub Desktop.
Run NetSuite SuiteQL queries using REST API in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import pandas as pd | |
import os | |
from oauthlib import oauth1 | |
from requests_oauthlib import OAuth1Session | |
def parse_suiteql_response(response): | |
response_json = json.loads(response.text) | |
items = response_json['items'] | |
offset = response_json['offset'] | |
count = response_json['count'] | |
total = response_json['totalResults'] | |
if response_json["hasMore"]: | |
next_link = next(link for link in response_json["links"] if link["rel"] == "next")["href"] | |
else: | |
next_link = None | |
return items, offset, count, total, next_link | |
def run_suiteql_query(query): | |
client = OAuth1Session( | |
client_secret=os.environ["netsuite_consumer_secret"], | |
client_key=os.environ["netsuite_consumer_key"], | |
resource_owner_key=os.environ["netsuite_access_token"], | |
resource_owner_secret=os.environ["netsuite_token_secret"], | |
realm=os.environ["netsuite_account"], | |
signature_method=oauth1.SIGNATURE_HMAC_SHA256 | |
) | |
url_account = os.environ["netsuite_account"].replace("_", "-").replace("SB", "sb") | |
url = f"https://{url_account}.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql" | |
headers = { | |
"Prefer": "transient", | |
"Content-Type": "application/json" | |
} | |
body = json.dumps({"q": query}) | |
data = [] | |
while True: | |
response = client.post(url=url, data=body, headers=headers) | |
try: | |
response.raise_for_status() | |
except requests.exceptions.HTTPError as e: | |
raise Exception(f"SuiteQL request failed. {e}. Response Body: {response.text}") | |
items, offset, count, total, next_link = parse_suiteql_response(response) | |
print(f"Retrieved {offset + count} of {total} results") | |
data = data + items | |
if next_link: | |
url = next_link | |
else: | |
break | |
return pd.json_normalize(data) | |
run_suiteql_query(""" | |
SELECT | |
'Hello World' AS greeting | |
FROM | |
DUAL | |
""") |
Do you know which objects/streams/tables are available to be used in the queries? I mean, customer is available... invoice seem to not be available...
Tim has a page with close to 800 tables and the permissions which may be useful:
https://timdietrich.me/blog/netsuite-suiteql-tables-permissions-reference/
Alternatively, if you can setup his query tool in an account you should be able to use the tables link in that.
Or navigate in NetSuite to Setup -> Records Catalog
This is helpful. Can this be modified to query a saved search instead of direct tables?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you know which objects/streams/tables are available to be used in the queries? I mean, customer is available... invoice seem to not be available...