Last active
July 14, 2025 11:30
-
-
Save ohri-anurag/b54f9356b5143c8af2909a331bb8b467 to your computer and use it in GitHub Desktop.
Shell script for NetSuite SOAP requests
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
| source .env.NetsuiteStaging | |
| soap () { | |
| timestamp=$(date -u +%s) | |
| nonce=$(openssl rand -hex 8) | |
| baseString="${ACCOUNT}&${CONSUMER_KEY}&${TOKEN_ID}&${nonce}&${timestamp}" | |
| key="${CONSUMER_SECRET}&${TOKEN_SECRET}" | |
| signature=$(echo -n $baseString | openssl sha-256 -binary -hmac $key | base64) | |
| accountInUrl=$(echo $ACCOUNT | sed s/_/-/ ) | |
| input=$(echo $1 | cut -d. -f1) | |
| output="${input}-output.xml" | |
| sed -e s/{{ACCOUNT}}/$ACCOUNT/ -e s/{{CONSUMER_KEY}}/$CONSUMER_KEY/ -e s/{{TOKEN_ID}}/$TOKEN_ID/ -e s/{{nonce}}/$nonce/ -e s/{{timestamp}}/$timestamp/ -e s@{{signature}}@$signature@ $1 | curlie -o $output https://$accountInUrl.suitetalk.api.netsuite.com/services/NetSuitePort_2023_2 SOAPAction:$2 | |
| xmllint --format -o $output $output | |
| } | |
| urlencode () { | |
| echo $1 | jq -rR @uri | |
| } | |
| rest () { | |
| signatureMethod="HMAC-SHA256" | |
| timestamp=$(date -u +%s) | |
| nonce=$(openssl rand -hex 10) | |
| oauthParams=$(jq -c <<EOF | |
| { | |
| "oauth_consumer_key": "${CONSUMER_KEY}", | |
| "oauth_nonce": "${nonce}", | |
| "oauth_signature_method": "${signatureMethod}", | |
| "oauth_timestamp": "${timestamp}", | |
| "oauth_token": "${TOKEN_ID}", | |
| "oauth_version": 1.0 | |
| } | |
| EOF | |
| ) | |
| method=$(jq -r '.method' $1) | |
| resource=$(jq -r '.resource' $1) | |
| queryParams=$(jq '.queryParams' $1) | |
| allParams=$(echo $queryParams | jq --argjson oauthParams "$oauthParams" -S '. + $oauthParams') | |
| accountInUrl=$(echo $ACCOUNT | sed s/_/-/ | tr '[:upper:]' '[:lower:]') | |
| url="https://$accountInUrl.suitetalk.api.netsuite.com/services/rest/record/v1/$resource" | |
| baseUrl=$(urlencode $url) | |
| queryParamsArray=() | |
| while IFS= read -r key; do | |
| value=$(echo "$queryParams" | jq --arg key $key -r ".$key") | |
| encodedValue=$(echo "$value" | sed -e "s/ /%20/g") | |
| queryParamsArray+=("${key}=${encodedValue}") | |
| done < <(echo $queryParams | jq -r 'keys[]') | |
| if [[ ${#queryParamsArray[@]} -gt 0 ]]; then | |
| url+="?" | |
| url+=$(IFS="&"; echo "${queryParamsArray[*]}") | |
| echo $url | |
| fi | |
| paramsArray=() | |
| while IFS= read -r key; do | |
| value=$(echo "$allParams" | jq --arg key $key -r ".$key") | |
| encodedKey=$(urlencode "$key") | |
| encodedValue=$(urlencode "$value") | |
| paramsArray+=("${encodedKey}=${encodedValue}") | |
| done < <(echo "$allParams" | jq -r 'keys[]') | |
| params=$(IFS="&"; echo "${paramsArray[*]}") | |
| encodedParams=$(urlencode $params) | |
| key="${CONSUMER_SECRET}&${TOKEN_SECRET}" | |
| baseString="${method}&${baseUrl}&${encodedParams}" | |
| signature=$(echo -n $baseString | openssl sha-256 -binary -hmac $key | base64 | jq -rR @uri) | |
| authorizationHeader="OAuth realm=\"${ACCOUNT}\",oauth_consumer_key=\"${CONSUMER_KEY}\",oauth_token=\"${TOKEN_ID}\",oauth_signature_method=\"${signatureMethod}\",oauth_timestamp=\"${timestamp}\",oauth_nonce=\"${nonce}\",oauth_version=\"1.0\",oauth_signature=\"${signature}\"" | |
| output="$1-output.json" | |
| body=$(jq '.body' $1) | |
| if [ "$body" = "null" ]; then | |
| echo $body | curlie $method $url Authorization:"$authorizationHeader" | jq > $output | |
| else | |
| curlie $method $url Authorization:"$authorizationHeader" | jq > $output | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment