NetSuite Python Client
The `netsuite` Python library provides an asynchronous client for interacting with NetSuite's SuiteTalk SOAP/REST Web Services and Restlets. It simplifies making requests, handling authentication (including Token-Based Authentication), and parsing responses. Currently at version 0.12.0, the library has a moderately active release cadence, with several minor releases and occasional breaking changes.
Common errors
-
TypeError: object asyncio.coroutines.CoroWrapper can't be used in 'await' expression
cause Attempting to call an asynchronous method (e.g., `ns.rest_api.get()`) without `await` in an `async` context or outside an `asyncio` loop.fixPrepend `await` to the async call (e.g., `await ns.rest_api.get(...)`) and ensure your code is running within an `async` function or `asyncio.run()`. -
netsuite.exceptions.NetSuiteAPIError: Authentication Failure
cause Incorrect or missing NetSuite Token-Based Authentication (TBA) credentials (account ID, consumer key/secret, token ID/secret).fixCheck that `NETSUITE_ACCOUNT`, `NETSUITE_CONSUMER_KEY`, `NETSUITE_CONSUMER_SECRET`, `NETSUITE_TOKEN_ID`, and `NETSUITE_TOKEN_SECRET` environment variables are correctly set and correspond to a valid NetSuite TBA setup. -
AttributeError: 'NetSuite' object has no attribute 'some_old_method'
cause Attempting to use a method or attribute that has been removed or renamed in a newer version of the `netsuite` library, especially after a major upgrade like 0.10.0.fixRefer to the latest documentation and `CHANGELOG.md` for your installed version. Update your code to use the current API methods and object structures.
Warnings
- breaking Version 0.10.0 introduced 'large breaking changes' due to significant updates of underlying libraries (e.g., aiohttp, zeep, lxml). This may affect existing code relying on specific dependency versions or low-level interactions.
- breaking Version 0.7.0 was a 'beta' release that introduced 'a lot of things' that break previous functionality. The documentation explicitly advised reviewing the `CHANGELOG.md` carefully for migration.
- gotcha The `netsuite` library is built entirely on `asyncio`. All API calls and client methods that interact with NetSuite must be `await`ed within an `async` function or an `asyncio` event loop.
- gotcha Authentication primarily relies on Token-Based Authentication (TBA) credentials provided via environment variables (NETSUITE_ACCOUNT, NETSUITE_CONSUMER_KEY, NETSUITE_CONSUMER_SECRET, NETSUITE_TOKEN_ID, NETSUITE_TOKEN_SECRET). Missing or incorrect variables will lead to authentication failures.
Install
-
pip install netsuite
Imports
- NetSuite
from netsuite import NetSuite
Quickstart
import asyncio
import os
from netsuite import NetSuite
async def main():
# Ensure these environment variables are set:
# NETSUITE_ACCOUNT, NETSUITE_CONSUMER_KEY, NETSUITE_CONSUMER_SECRET,
# NETSUITE_TOKEN_ID, NETSUITE_TOKEN_SECRET
# The NetSuite client automatically picks up credentials from environment variables.
# For explicit config, see documentation.
ns = NetSuite()
print(f"NetSuite client initialized for account: {os.environ.get('NETSUITE_ACCOUNT', '[Not Set]')}")
try:
# Example: Fetch a specific record using the REST API
# Replace 'customer' and '1234' with actual record type and ID in your NetSuite instance
customer_id = '1234'
customer_record = await ns.rest_api.get(
f"record/v1/customer/{customer_id}"
)
print(f"Successfully fetched customer {customer_id}:\n{customer_record}")
# Example: Run a SuiteQL query
# This query selects the ID and company name of the first 5 customers
suiteql_result = await ns.suiteql.get(
"SELECT id, companyname FROM customer ORDER BY id LIMIT 5"
)
print(f"\nSuccessfully executed SuiteQL query (first 5 customers):\n{suiteql_result}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
# Set dummy environment variables for demonstration if not set
os.environ.setdefault('NETSUITE_ACCOUNT', 'TEST_ACCOUNT_ID')
os.environ.setdefault('NETSUITE_CONSUMER_KEY', 'TEST_CONSUMER_KEY')
os.environ.setdefault('NETSUITE_CONSUMER_SECRET', 'TEST_CONSUMER_SECRET')
os.environ.setdefault('NETSUITE_TOKEN_ID', 'TEST_TOKEN_ID')
os.environ.setdefault('NETSUITE_TOKEN_SECRET', 'TEST_TOKEN_SECRET')
asyncio.run(main())