# Acumatica REST API Python Script Generator

> Source: https://netray.co/tools/acumatica-rest-python-upsert-script-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Produce a ready-to-run Python script for the Acumatica contract-based REST API: login, PUT a record, then logout.

## Category

developer

## Erp

Acumatica

## Audience

- Acumatica developer
- Integration engineer

## Tool

**Kind:** template

**Output:** code

**Language:** python

**Fields:**

#### Acumatica host

**Key:** host

**Type:** text

**Default:** https://mycompany.acumatica.com

#### Default endpoint version

**Key:** endpoint_ver

**Type:** text

**Default:** 22.200.001

#### Tenant

**Key:** tenant

**Type:** text

**Default:** MyTenant

#### Branch

**Key:** branch

**Type:** text

**Default:** MAIN

#### Entity

**Key:** entity

**Type:** select

**Options:**

- StockItem
- Customer
- Vendor
- SalesOrder

**Default:** StockItem

#### Key field name

**Key:** key_field

**Type:** text

**Default:** InventoryID

#### Key value

**Key:** key_value

**Type:** text

**Default:** BRKT-1001

#### Description

**Key:** desc_value

**Type:** text

**Default:** Mounting bracket, 2 mm steel

**Blocks:**

#### Python script

**Body:** import requests

HOST = '{{host}}'
VER = '{{endpoint_ver}}'
BASE = HOST + '/entity/Default/' + VER

s = requests.Session()
login = s.post(HOST + '/entity/auth/login', json={
    'name': 'integration.user',
    'password': 'CHANGE_ME',
    'tenant': '{{tenant}}',
    'branch': '{{branch}}',
})
login.raise_for_status()

try:
    body = {
        '{{key_field}}': {'value': '{{key_value}}'},
        'Description': {'value': '{{desc_value}}'},
    }
    r = s.put(BASE + '/{{entity}}', json=body)
    r.raise_for_status()
    print(r.status_code, r.json().get('{{key_field}}'))
finally:
    s.post(HOST + '/entity/auth/logout')

**Notes:** A PUT on the contract-based REST API inserts the record or updates it when the key already exists. Entity and field names depend on the endpoint version and on your customizations, so check them in the Web Service Endpoints screen. Keep credentials in a secrets store, never in the script, and always log out because open sessions count against your license. SalesOrder needs more required fields than shown.

## Content

**Intro:** Acumatica's contract-based REST API is the standard way to push items, customers and orders into the system. This generator writes a safe skeleton you can adapt, with login and logout handled properly.

**Sections:**

#### How to use the script

**Body:** Fill in your host, tenant, branch, endpoint version and the entity you want to write. Copy the script, replace the credential placeholders with values from a secrets store, and run it against a test tenant first. The script logs in with a cookie session, sends a PUT with the field values wrapped in value objects, and logs out in a finally block so a failure never leaves a session open.

#### How the API behaves

**Body:** Each field in the JSON body is an object with a value property. A PUT acts as an upsert on the entity key. Related details such as order lines are sent as arrays of objects. The default endpoint and its version are listed on the Web Service Endpoints screen, and you can extend the contract with your own endpoint when you need custom fields.

**Bullets:**

- Use a test tenant before touching production
- One integration user per interface helps auditing
- Add retries with a delay for transient 5xx responses

#### Common mistakes

**Body:** Frequent problems are forgetting to log out, which can exhaust concurrent API user limits, sending plain values instead of value objects, and using a version number that does not exist on the instance. Also remember that required fields vary with your item class and numbering setup, so a minimal body that works for one class can fail for another.

**Cta:** If you are building several of these integrations, we can help design and test them against your tenant. Ask about an integration review.

**Faq:**

#### Does a PUT create or update in Acumatica?

**Answer:** Both. If the key value does not exist the record is created, and if it does exist the supplied fields are updated. That makes it convenient for sync jobs, but you should still confirm required fields.

#### How do I find the endpoint version?

**Answer:** Open the Web Service Endpoints screen and look at the Default endpoint. The version is the number in the URL path, and it changes with major releases unless you pin your own endpoint.

#### Why must I log out?

**Answer:** Sessions count against the number of concurrent API users in your license. Forgetting to log out can lock other integrations out until the sessions expire.

#### Can I use OAuth instead of a cookie login?

**Answer:** Yes, Acumatica supports OAuth 2.0 flows with a connected application. This script uses the simple cookie login for clarity, and a production design should consider OAuth.

## Search terms

acumatica rest api python, acumatica api example python, acumatica contract based rest, acumatica create stock item api, acumatica api login logout, acumatica integration script

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
