Skip to main content

Python client for SugarCRM API

Project description

Python client for SugarCRM API.

Example Usage

import sugarcrm

# Connect
url = "http://your-sugarcrm-domain/service/v4/rest.php"
session = sugarcrm.Session(url, username, password)

# Create a new note
note = sugarcrm.Note(name="Test Note")

# Save note

# Add attachment to note
session.set_note_attachment(note, "")

# Query for all notes that have a name that begins with "Test"
note_query = sugarcrm.Note(name="Test%")
results = session.get_entry_list(note_query)

# Query for all contacts with the first name "Mylee"
contact_query = sugarcrm.Contact(first_name="Mylee")
results = session.get_entry_list(contact_query)

# Get the email address for the user assigned to an Opportunity
op = session.get_entry("Opportunities", "82f72939-735e-53a2-0944-5418c4edae2a")
user = session.get_entry("Users", op.assigned_user_id)
print user.email1

# Change the status of an Opportunity
op = sugarcrm.Opportunity(id="82f72939-735e-53a2-0944-5418c4edae2a")
op.sales_stage = "Approved"

# Extract all non-empty email fields from all Contacts in SugarCRM
emails = set()
contact_query = sugarcrm.Contact()  # No filters provider finds all objects
contact_count = session.get_entries_count(contact_query, deleted=True)
print "Extracting emails from %d Contacts" % contact_count
# Grab 100 Contact objects at a time from SugarCRM
for offset in range(0, count, 100):
    contacts = session.get_entry_list(contact_query, deleted=True,
                                    max_results=100, offset=offset)
    for contact in contacts:
        for field in dir(contact):
            if field.find("email") >= 0 and getattr(contact, field, "").find("@") >= 0:
                emails.add(getattr(contact, field).lower())
print "Found %d emails" % len(emails)


The latest stable version can always be installed or updated via pip:

$ pip install sugarcrm

If the above fails, please use easy_install instead:

$ easy_install sugarcrm

Session Object

class sugarcrm.Session(url, username, password, app=”Python”, lang=”en_us”, verify=True)
The main class used to connect to the SugarCRM API and make requests with.
url = "http://your-sugarcrm-domain/service/v4/rest.php"
session = sugarcrm.Session(url, username, password)

The verify parameter is passed directly to the python-requests library that is used to make HTTP POST requests to the SugarCRM API. Read more about this parameter from the python-requests documentation:

Available Methods

Retrieves a list of available modules in the system. Possible filter values: “default”, “mobile”, “all”
modules = session.get_available_modules()
for m in modules:
    print m.module_key
get_entry(module, object_id, links={}, track_view=False)
Retrieves a single object based on object ID.
note = session.get_entry("Notes", "f0c78aab-e051-174a-12aa-5439a7146977")

# Get a lead and specific fields from linked contacts in one query
links = {'Contacts': ['id', 'first_name', 'last_name']}
lead = session.get_entry("Leads", "d7dac88d-ce33-d98a-da8b-5418bba9e664",
for c in lead.contacts:
    print, c.first_name, c.last_name
get_entries(module, object_ids, track_view=False)
Retrieves a list of objects based on specified object IDs.
ids = [
notes = session.get_entries("Notes", ids)
for note in notes:
get_entries_count(query_object, deleted=False)
Retrieves a count of beans based on query specifications.
# Get a count of all Contacts with a first name of "Fred"
# and include Contacts that have been deleted
contact_query = sugarcrm.Contact(first_name="Fred")
contacts = session.get_entries_count(contact_query, deleted=True)
for contact in contacts:
    print contact.first_name, contact.last_name
get_entry_list(query_object, fields=[], links={}, order_by=””, max_results=0, offset=0, deleted=False, favorites=False)
Retrieves a list of objects based on query specifications.
# Get a list of all Notes with a name that begins with "Test"
note_query = sugarcrm.Note(name="Test%")
notes = session.get_entry_list(note_query)
for note in notes:

# Get a list of all Opportunities created since Sept 1, 2014 and include
# data about link contacts with each Opportunitity returned
q = sugarcrm.Opportunity()
q.query = "opportunities.date_entered > '2014-09-01'"
links = {'Contacts': ['id', 'first_name', 'last_name']}
results = session.get_entry_list(q, links=links)
for o in results:
    for c in o.contacts:
        print,, c.first_name, c.last_name
login(username, password, app=”Python”, lang=”en_us”)
Logs a user into the SugarCRM application.
set_document_revision(document, file)
Creates a new document revision for a specific document record.
doc = sugarcrm.Document(document_name="Test Doc", revision=1)
session.set_document_revision(doc, "/path/to/test.pdf")
Creates or updates a specific object.
note = sugarcrm.Note() = "Test Note"
note.assigned_user_id = "82f72939-735e-53a2-0944-5418c4edae2a"
set_note_attachment(note, attachment)
Creates an attachmentand associates it to a specific note object.
with open("test1.pdf") as pdf_file:
    session.set_note_attachment(note1, pdf_file)
session.set_note_attachment(note2, "test2.pdf")
print note1.filename, note2.filename
set_relationship(parent, child, delete=False)
Sets the relationships between two records.
doc = sugarcrm.Document(document_name="Test Doc", revision=1)
session.set_document_revision(doc, "/path/to/test.pdf")
opportunity = session.get_entry("Opportunities", "5b671886-cfe4-36f5-fa9d-5418a24e4aca")
session.set_relationship(opportunity, doc)

Unavailable Methods

The following lesser-used SugarCRM API methods have not been included in this library yet. Please open an issue if you require any of these methods and I would be more than happy to implement them!

Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.
Method not implemented yet.

SugarCRM Objects

>>> call = sugarcrm.Call()
>>> print call.module

>>> campaign = sugarcrm.Campaign()
>>> print campaign.module

>>> contact = sugarcrm.Contact()
>>> print contact.module

>>> document = sugarcrm.Document()
>>> print document.module

>>> email = sugarcrm.Email()
>>> print email.module

>>> lead = sugarcrm.Lead()
>>> print lead.module

>>> module = sugarcrm.Module()
>>> print module.module

>>> note = sugarcrm.Note()
>>> print note.module

>>> opportunity = sugarcrm.Opportunity()
>>> print opportunity.module

>>> product = sugarcrm.Product()
>>> print product.module

>>> prospect = sugarcrm.Prospect()
>>> print prospect.module

>>> prospect_list = sugarcrm.ProspectList()
>>> print prospect_list.module

>>> quote = sugarcrm.Quote()
>>> print quote.module

>>> report = sugarcrm.Report()
>>> print report.module

>>> user = sugarcrm.User()
>>> print user.module

Development Version

The latest development version can be installed directly from GitHub:

$ pip install --upgrade


Issues and Pull Requests are always welcome.


Code and documentation are available according to the MIT License (see LICENSE).

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sugarcrm, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size sugarcrm-0.1.2.tar.gz (9.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page