REST API

Using a Python Client

Updated: July 17, 2023

We have developed a small Python library that implements the main functions of the JSON-RPC API. You can check out the blog post A sample Python library for the Nuxeo Content Automation JSON-RPC API.

Alternatively you can use the standard library of Python to access a Nuxeo repository using the Content Automation API. Here is a worked example with screencast that demonstrates how to deploy a custom server side operation developed in Java using the Nuxeo IDE and a client Python script that calls it: Exploring Nuxeo APIs: Content Automation.

Here is an example building a document query using Python:

#!/usr/bin/env python

import urllib2, base64

QUERY_URL = "http://localhost:8080/nuxeo/site/automation/Document.Query"
USER = 'Administrator'
PASSWD = 'Administrator'

auth = 'Basic %s' % base64.b64encode(USER + ":" + PASSWD).strip()
headers = {
   "Content-Type": "application/json+nxrequest",
   "Authorization": auth}
data = '{params: {"query": "SELECT * FROM Document"}, context : {}}'
req = urllib2.Request(QUERY_URL, data, headers)

resp = urllib2.urlopen(req)

print resp.read()

Here's a slightly more involved example, that illustrates:

  • How to use a HTTPCookieProcessor for keeping session state,
  • The use of the input parameter,
  • A few different document-related commands (Document.Query, Document.Create, Document.Delete).
#!/usr/bin/env python

import urllib2, base64, sys
import simplejson as json
from pprint import pprint

URL = "http://localhost:8080/nuxeo/site/automation/"
USER = 'Administrator'
PASSWD = 'Administrator'

cookie_processor = urllib2.HTTPCookieProcessor()
opener = urllib2.build_opener(cookie_processor)
urllib2.install_opener(opener)

def execute(command, input=None, **params):
    auth = 'Basic %s' % base64.b64encode(USER + ":" + PASSWD).strip()
    headers = {
        "Content-Type": "application/json+nxrequest",
        "Authorization": auth}
    d = {}
    if params:
        d['params'] = params
    if input:
        d['input'] = input
    if d:
        data = json.dumps(d)
    else:
        data = None
    req = urllib2.Request(URL + command, data, headers)

    try:
        resp = opener.open(req)
    except Exception, e:
        exc = json.load(e.fp)
        print exc['message']
        print exc['stack']
        sys.exit()
    s = resp.read()
    if s:
        return json.loads(s)
    else:
        return None

print "All automation commands:"
print
for op in execute("")['operations']:
    pprint(op)
    pprint
print

print "All documents in the repository:"
print
for doc in execute("Document.Query", query="SELECT * FROM Document")['entries']:
    print doc['path'], ":", doc['type']
print

print "Fetch workspaces root:"
doc = execute("Document.Fetch", value="/default-domain/workspaces")
pprint(doc)
print

print "Create a new doc:"
new_doc = execute("Document.Create", input="doc:" + doc['uid'], type="Workspace", name="MyWS",
        properties="dc:title=My new workspace")
print

print "Delete new doc:"
execute("Document.Delete", input="doc:" + new_doc['uid'])
print