API
API¶
The plugin API is under /api/plugins/contracts/.
note: When creating invoices and invoice lines through the API, the corresponding contracts respectively accounting dimensions, must be referenced as a list of id.
Every list endpoint accepts the standard NetBox filters (id, q, tag, created, last_updated, ...) and the filters of its model, for example serviceproviders/?name=, contracttype/?name=, accountingdimension/?name=&value=&status= and contractassignment/?contract=.
Units and contract lines¶
units/:name,description,billing_method(one_time,recurring,usage),months(required forrecurring, empty otherwise). Deleting a unit used by contract lines returns 409.contract-lines/:contract(id),description,quantity,unit_price,unit(id),currency(defaults to the contract's),start_dateandend_date(default to the contract's),accounting_dimensions(list of ids), and the read-onlytotal_value,yearly_valueandinvoiced_at_conversion. Filters:contract_id,unit_id,unit,currency,billing_method,accounting_dimensions,invoice_id,q. Creating or deleting a line of a contract that has invoices, or changing its contract terms, returns 400; itsaccounting_dimensions,commentsandtagscan still be changed. The read-onlyreplacesfield shows the line a line replaces after an amendment.POST contract-lines/{id}/amend/witheffective_date,reasonand a newunit_priceand/orquantity: ends the line the day beforeeffective_dateand returns the new line (201). Requires the view and amend actions on that line (object permissions, constraints included); a write-enabled token is needed. A line outside the view constraints returns 404, a user without the amend action 403. Errors (date before the end of the last invoiced period, one-time line, missing reason) return 400.
Contracts¶
billable(writable, defaulttrue) and the read-only valuestotal_contract_value(null when not available),yearly_contract_valueandyearly_billable_value. Filter:billable.mrc,yrcandnrcare deprecated and kept for compatibility.
Invoices¶
- A new invoice is linked to at most one contract and has its currency. Its
statusdefaults todraft. - A Posted invoice cannot change its
amount,currency,period_start,period_endorcontracts(400); itsstatuscan change. POST invoices/generates the invoice lines of a new invoice from the contract lines, in the same request. It returns 400 for a non-billable contract, or whenamountis lower than the total of the lines to generate. Editing an invoice never generates lines.templateis deprecated: invoice templates are no longer copied.
Invoice lines¶
contract_line(id, optional),unit(id) andunit_price(default to those of the contract line),quantity.- Lines of a Posted invoice cannot be created or deleted, and their unit, unit price, quantity, amount, currency and contract line cannot change (400).
amountis always quantity x unit price and ignored if sent;unit_priceis required, except that anamountgiven alone when a line is created is taken as its unit price with quantity 1 (compatibility).quantitydefaults to 1.
GraphQL¶
The plugin's objects are in NetBox's GraphQL API (/graphql/), with two queries per object type: contract / contract_list, contract_line / contract_line_list, contract_type / contract_type_list, contract_assignment / contract_assignment_list, invoice / invoice_list, invoice_line / invoice_line_list, unit / unit_list, accounting_dimension / accounting_dimension_list and service_provider / service_provider_list. Results follow the user's view permissions, constraints included.
{
contract_list(filters: {status: {exact: STATUS_ACTIVE}, currency: {exact: "usd"}}) {
name
contract_type { name }
external_party_object { __typename ... on ServiceProviderType { name } ... on ProviderType { name } }
lines { description quantity unit_price unit { name } }
invoices { number status }
}
}
- Every stored field and relation is available.
mrc,yrc,nrc(contracts) andtemplate(invoices) are marked deprecated. - The computed values (
total_contract_value,yearly_contract_valueandyearly_billable_valueof contracts,total_valueandyearly_valueof contract lines) are not in GraphQL: read them from the REST API. external_party_objectof a contract is a service provider or a circuit provider.content_objectof a contract assignment covers the models of the defaultsupported_modelssetting (circuits, virtual circuits, sites, devices, racks, virtual machines, clusters); for an object of another model it isnull, andcontent_typeandobject_ididentify it.
Nested objects and brief=true¶
Related objects are nested in the responses:
invoiceof an invoice line:id,url,display,number.accounting_dimensionsof contract lines and invoice lines:id,url,display,name,value.replacesof a contract line: the brief contract line (id,url,display,contract,description,quantity,unit,unit_price,currency,start_date,end_date).contractof contract assignments and contract lines, andparentof contracts: the nested contract, with the fields listed in the next section plusid,url,display,nameandstatus.contract_typeis the id of the contract type.contractsof an invoice: the full contracts.
A related object can be written by id, or by a dictionary of attributes that identifies exactly one object (for example "contract": {"name": "C-1"}).
?brief=true on a list returns the brief representation of each object, for example id, url, display, invoice, accounting_dimensions, amount and currency for invoice lines.
Deprecated nested fields¶
A later release will reduce the nested contract and the brief contract to id, url, display, name and status, the brief invoice to id, url, display and number, and the contracts of an invoice to brief contracts. To prepare, read the fields you need from contracts/{id}/ (or invoices/{id}/) instead of the nested objects. The fields that will be removed are:
| Where | Fields that will be removed |
|---|---|
Nested contract (contract of assignments and contract lines, parent of contracts) |
contract_type, external_party_object_type, external_party_object_id, external_party_object, external_reference, internal_party, tenant, start_date, end_date, initial_term, renewal_term, notice_period, currency, mrc, yrc, nrc, invoice_frequency, comments, documents |
contracts of an invoice |
the fields above, plus billable, total_contract_value, yearly_contract_value, yearly_billable_value, parent, tags, custom_fields, created, last_updated |
contracts/?brief=true |
contract_type, external_party_object_type, external_party_object_id, external_party_object, external_reference, internal_party, tenant, start_date, end_date, initial_term, renewal_term, currency, mrc, yrc, nrc, invoice_frequency, billable, comments, parent |
invoices/?brief=true |
date, template, contracts, period_start, period_end, currency, amount, comments |