NetSuite integrations Table of contents: Articles

How to connect to NetSuite’s SuiteTalk REST API using Python

Connecting to NetSuite’s SuiteTalk REST API can be complicated.

The Python code below has been tested and works 100%. It uses token-based authentication (TBA) to connect to the NetSuite REST API.

First, install the necessary libraries:

pip install requests requests-oauthlib

Create a new code file with the code listed below. Type in your account ID, consumer key, consumer secret, token ID, token secret and company URL in the ‘NetSuite TBA credentials’ section.

Then run the code with:

python FILENAME.py

It should work!


import requests
import oauthlib.oauth1
from requests_oauthlib import OAuth1
import urllib.parse

# NetSuite TBA credentials
# account_id = 'YOUR_ACCOUNT_ID'  # Your NetSuite account ID, eg. 123456 or 123456_SB1
# consumer_key = 'YOUR_CONSUMER_KEY'  # Your Consumer Key from NetSuite
# consumer_secret = 'YOUR_CONSUMER_SECRET'  # Your Consumer Secret from NetSuite
# token_key = 'YOUR_TOKEN_KEY'  # Your Token ID from NetSuite
# token_secret = 'YOUR_TOKEN_SECRET'  # Your Token Secret from NetSuite
# rest_api_url = 'YOUR_REST_API_URL' # The REST API URL eg. https://123456.suitetalk.api.netsuite.com or https://123456-sb1.suitetalk.api.netsuite.com


# Base URL for this API call
base_url = f'{rest_api_url}/services/rest/record/v1/customer'

query_params = {
    'limit': 10, 
    'offset': 0 
}

# Properly encode the query parameters 
encoded_query_string = urllib.parse.urlencode(query_params, safe='!\'()*')

# Full URL with encoded query parameters
full_url = f'{base_url}?{encoded_query_string}'

# Create OAuth1 session
oauth = OAuth1(
    client_key=consumer_key,
    client_secret=consumer_secret,
    resource_owner_key=token_key,
    resource_owner_secret=token_secret,
    realm=account_id,
    signature_method=oauthlib.oauth1.SIGNATURE_HMAC_SHA256,
)

try:
    response = requests.get(full_url, auth=oauth, headers={'Content-Type': 'application/json'})

    # Print the response data
    print('Response data:', response.json())

except requests.exceptions.RequestException as error:
    # Error handling
    print(f'Error: {error}')