Easily access your billing data with the Postman API
The new Billing endpoints on the Postman API make it easy to retrieve information about your Postman account’s billing and invoice details—and integrate Postman with your ERP system. In this blog post, we’ll walk through how you can use these endpoints effectively.
Getting started with the Billing endpoints
First, get your account details with the GET /accounts
endpoint:
const axios = require('axios'); const apiKey = 'your_api_key'; // Replace with your actual API key const config = { headers: { Authorization: `Bearer ${apiKey}` } }; axios.get('https://api.postman.com/accounts', config) .then(response => { console.log(response.data); }) .catch(error => { console.error('Error fetching account details:', error); });
This endpoint returns information such as your account ID, team ID, account status, slots, and your billing email address:
{ "id": 123456, "teamId": 654321, "state": "PAID", "slots": { "total": 10, "unbilled": 0, "consumed": 2, "available": 8 }, "salesChannel": "SELF_SERVE", "billingEmail": "[email protected]" }
Make sure to save the id
(account ID) value because you’ll need it for requests to get your invoice history.
Get your invoice history
To get your invoice history, use the GET /accounts/{accountId}/invoices
endpoint. Use the id
value (for example, 123456
) from the GET /accounts
endpoint response for the accountId
value:
const accountId = 'your_account_id'; axios.get(`https://api.postman.com/accounts/${accountId}/invoices`, config) .then(response => { console.log(response.data); }) .catch(error => { console.error('Error fetching invoice history:', error); });
This returns a list of invoices, including their status, issue date, total amount, and a link to view them:
{ "data": [ { "id": "inv_7UDSYBJPGQU93N7M", "status": "PAID", "issuedAt": "2023-10-12", "totalAmount": { "value": 440, "currency": "USD" }, "links": { "web": { "href": "https://pay.postman.com/invoices/pay?invoice_public_id=inv_7UDSYBJPGQU93N7M" } } } ] }
Handling errors
Our API is designed to be user-friendly, but here are some common errors to be aware of:
- No token provided (403): Ensure your API key is correctly included in the header.
- Invalid accountId (403): Verify that the account ID is correct.
- Insufficient role (403): Check if your account has the necessary permissions.
- No status provided (400): Currently we only support invoices whose status is PAID.
- No team (400): You must be a member of a team to use these endpoints.
Importing invoice information to SAP
Integrating invoice data from an API into SAP systems can be a complex task, as it typically involves several steps and depends on the specific SAP setup you have. SAP systems often use different methods for data import, such as IDocs, BAPIs, or direct database insertion. For simplicity and general applicability, we will provide a basic example using a common approach: sending data to SAP with an OData service.
Prerequisites:
- SAP setup: Ensure your SAP system has OData services enabled and that you have the permissions to use them.
- Invoice data: Get your invoice data from the Postman Billing API.
- An SAP OData endpoint: Ensure that you have an OData service endpoint in your SAP system that accepts invoice data. This usually requires SAP development (ABAP) to create the service.
Step 1: Create a SAP OData service
Creating an OData service in SAP with ABAP involves several steps, including defining a data model, implementing service operations, and potentially modifying the backend to support these operations. The following is a simplified example of how you might create an OData service to handle the specified payload. This example assumes familiarity with SAP’s ABAP development environment and tools like the SAP Gateway Service Builder (transaction SEGW).
Step 2: Define the data model
Define a data model in the transaction SEGW. This model represents the structure of the invoice data you want to expose.
- In transaction SEGW, create a new project.
- Create an
Invoice
entity type with properties matching the JSON structure. For example:ID
(String)Status
(String)IssuedAt
(DateTime)Value
(Decimal)Currency
(String)Link
(String)
- Create an
Invoices
entity set for theInvoice
entity type.
Step 3: Implement the service
You’ve define the data model, so now you’ll need to implement the service’s runtime behavior:
- In the SEGW project, generate runtime objects.
- Implement the data provider class (DPC). Redefine the methods in the generated DPC class to get and send data. For this example, focus on the
CREATE_ENTITY
method to handle incoming POST requests. You can use the following ABAP code snippet for theCREATE_ENTITY
method:
METHOD /iwbep/if_mgw_appl_srv_runtime~create_entity. DATA: ls_invoice TYPE , " Define a structure corresponding to the Invoice entity lv_json TYPE string. " Deserialize the incoming JSON payload to the ABAP structure /iwbep/cl_mgw_req_util=>get_json_from_request( EXPORTING io_tech_request_context = io_tech_request_context IMPORTING ev_json = lv_json ). /ui2/cl_json=>deserialize( EXPORTING json = lv_json CHANGING data = ls_invoice ). " Implement logic to process the invoice data, e.g., insert into a database table " ... " Set the response copy_data_to_ref( EXPORTING is_data = ls_invoice CHANGING cr_data = er_entity ). " Set the HTTP status code to 201 (Created) set_response_status( iv_entity_name = 'Invoice' iv_status_code = '201' ). ENDMETHOD.
Step 4: Activate and test the service
After you implement the service, activate and test it with Postman or an OData client.
Example: Sending invoice data to SAP
The following example assumes you have an OData service set up in SAP to receive invoice data. The JavaScript code will be modified to send invoice data to this SAP OData service. Install Axios if you haven’t already:
const axios = require('axios'); // Replace with your actual API keys and endpoints const postmanApiKey = 'your_postman_api_key'; const sapODataEndpoint = 'your_sap_odata_service_endpoint'; const sapCredentials = { username: 'your_sap_username', password: 'your_sap_password' }; // Function to map the invoice data to the SAP data model function mapInvoiceToSAPModel(invoice) { return { ID: invoice.id, Status: invoice.status, IssuedAt: invoice.issuedAt, Value: invoice.totalAmount.value, Currency: invoice.totalAmount.currency, Link: invoice.links.web.href }; } // Fetch invoices from Postman API axios.get('https://api.postman.com/accounts/your_account_id/invoices', { headers: { Authorization: `Bearer ${postmanApiKey}` } }) .then(response => { const invoices = response.data.data; // Assuming the structure includes a 'data' field containing the invoices // Iterate over each invoice and send to SAP return Promise.all(invoices.map(invoice => { const sapInvoiceData = mapInvoiceToSAPModel(invoice); return axios.post(sapODataEndpoint, sapInvoiceData, { auth: sapCredentials }); })); }) .then(sapResponses => { sapResponses.forEach(resp => console.log('Invoice sent to SAP successfully:', resp.data)); }) .catch(error => { console.error('Error:', error); });
Important notes
We strongly recommend that you consider the following before integrating this example into your SAP systems:
- Data mapping: Ensure that the data structure the Postman API returns matches what your SAP OData service expects. You might need to transform the data before sending it to SAP.
- Authentication: The example uses basic authentication for SAP OData services. Your SAP system might use a different authentication method, like OAuth.
- Error handling: Proper error handling is crucial, especially when dealing with financial data. Make sure to implement robust error handling for both the Postman API call and the SAP OData service request.
- Security: Transmitting sensitive data like invoices requires careful handling of authentication credentials and data encryption. Always use secure methods to store and transmit credentials.
- Testing: Thoroughly test the integration in a non-production environment before rolling it out to production.
- SAP expertise: Integrating with SAP can be complex. If you’re not familiar with SAP systems, consider consulting with an SAP expert.
Next steps
These new Postman API endpoints streamline the process of managing your Postman account’s billing and invoice data—whether you’re a small business owner, a member of an accounting team, or a developer looking to integrate billing data into your systems. For details and updates, watch our Postman API collection.
Happy coding!
What do you think about this topic? Tell us in a comment below.