Welcome to the Apilo Help Center.
[API] Creating a sales document – address selection logic
Contents
How does the system select the billing address?
Key Address Fields Used in the Process (Reference to API)
Below is described the process of automatic selection of the billing address based on available data, with references to a typical API response structure. Understanding this logic is crucial for correctly generating sales documents and interpreting system operations.
## Key Address Fields Used in the Process (Reference to API)
The system operates based on several key address fields. In the API response, these are usually JSON objects.* **`addressInvoice`**:* **For the user:** A special place to enter the address that should appear on the invoice. If you want the invoice to be sent to a different address than the delivery address or the main contact address, it should be provided here.
* **For the developer:** In the API response, this is a JSON object, e.g., `response.addressInvoice`.
```jsonr
"addressInvoice": {r
"name": "Jan Kowalski",r
"phone": "+48 500 000 000",r
"email": "jan.kowalski@apilo.com",r
"streetName": "Testowa",r
"streetNumber": "4b/12",r
"city": "Kraków",r
"zipCode": "31-154",r
"country": "PL",r
"companyTaxNumber": "937-271-51-54", // NIP if the invoice is for a companyr
"companyName": "Apilo Sp. z o.o." // Company name if the invoice is for a company
}r
```
If this object exists (is not `null`) and contains data (e.g., is not an empty object `{}`), its content is used for the invoice. This field has the highest priority.* **`addressCustomer`**:* **For the user:** The main address associated with your account or customer data. This can be your residence or company’s registered address. The system distinguishes the type of this address.
* **For the developer:** In the API response, this is a JSON object, e.g., `response.addressCustomer`.
```jsonr
"addressCustomer": {r
"name": "Jan Kowalski",r
// ... other fields as abover
"companyTaxNumber": "937-271-51-54",r
"companyName": "Apilo Sp. z o.o."r
}r
```
The logic of the invoice is based on an additional conceptual field `class` (e.g., `response.addressCustomer.class`), which categorizes the type of address:r
* `”customer”`: Standard, general customer address.
* `”house”`: Interpreted as the customer’s home address.
* `”company”`: Interpreted as the customer’s business address.* **`addressDelivery`**:* **For the user:** The address to which the ordered product or service will be delivered.
* **For the developer:** In the API response, this is a JSON object, e.g., `response.addressDelivery`.
“`json
"addressDelivery": {r
"name": "Jan Kowalski",r
// ... other fields as abover
"parcelIdExternal": "KRA32B", // May contain pickup point datar
"parcelName": "Parcel locker, ul. Testowa 12 (next to the store)"r
}r
```
Invoice Address Selection Logic: Step by Step (Developer’s Perspective)
According to the new guidelines, the system utilizes the `isInvoice` field (found at the main level of the API response, e.g., `response.isInvoice`) and the availability of the `addressInvoice` object to determine which address will be used on the invoice.
**Main Logic Assumptions:**
* The `response.isInvoice` field (boolean type) signals whether a specific invoice address (`addressInvoice`) should be used for the given order, or, in the absence of that, the customer’s address (`addressCustomer`).
* If `response.isInvoice` is `true` and `response.addressInvoice` exists and contains data, then `response.addressInvoice` is the invoice address.
* If `response.addressInvoice` does not exist (is `null` or empty), the invoice address becomes `response.addressCustomer`. The role of `isInvoice` in this specific case is to signal the general need for invoicing, and the system selects the best available address according to this rule.**Address Selection Steps (for the developer):**1. **Check the dedicated invoice address (`response.addressInvoice`) in the context of the `isInvoice` flag:**
* **Condition:** If the `response.isInvoice` field is `true` AND the `response.addressInvoice` object exists and contains data (e.g., is not `null` and is not an empty object `{}`).
* For example, in JavaScript:
```javascriptr if (response.isInvoice === true && response.addressInvoice && Object.keys(response.addressInvoice).length > 0)r ```
* **Action:** The invoice address is the content of the `response.addressInvoice` object.
2. **Use the customer address (`response.addressCustomer`) as a primary or fallback:**
* **Condition:** If the above condition (using `response.addressInvoice`) is not met, meaning:
* `response.addressInvoice` does not exist or is empty (regardless of the `isInvoice` flag, if the goal is always to choose *some* address when an invoice is needed), OR
* `response.isInvoice` is `false` (in this scenario, if `isInvoice` = `false`, it means “use `addressCustomer` for the invoice”, not “do not generate an invoice” – this would require confirmation).
* **Action:** The invoice address is the content of the `response.addressCustomer` object.
* For example, in JavaScript (as part of the `else` instruction to the above `if`):
```javascriptr
else if (response.addressCustomer) {r
// Use response.addressCustomerr
}r
```
**Simplified Decision Scheme:**1. Does `response.addressInvoice` exist and contain data?
* **YES:**
* Is `response.isInvoice` `true`?
* **YES:** Use `response.addressInvoice`.
* **NO:** (Behavior not fully defined by the last description – whether to use `addressCustomer`, or not specifically invoice? Based on “data for the invoice is selected according to the isInvoice field, which determines which address should be chosen”, if `isInvoice` is `false`, then `addressInvoice` is not the “selected address”. The system probably falls back to `addressCustomer` or does not generate the invoice with those details). For consistency with the second condition, if `isInvoice` is `false`, `addressInvoice` is not used, and the system should check `addressCustomer` if an invoice is to be generated at all.
* **NO (`response.addressInvoice` is `null` or empty):**
* Use `response.addressCustomer`.
