SAP Integration with CubeMaster API
Step 1: Prerequisites
- Ensure you have a CubeMaster API Key. Sign up at CubeMaster.
- Obtain SAP system access with the required authorizations.
- Familiarity with SAP BTP (Business Technology Platform) and ABAP programming.
Before writing the ABAP code, review the CubeMaster API documentation to understand:
- The endpoints you need to call (e.g.,
https://api.cubemaster.net/loads
). - The authentication method.
- The request/response format (e.g., JSON).
- Any required headers or query parameters.
Step 2: Set Up HTTP Destination in SAP
Create an HTTP destination in SAP using Transaction Code: SM59.
- Destination Name:
Z_CUBEMASTER_API
- Target Host:
api.cubemaster.net
- Service No.:
443
(for HTTPS)
Step 3: Write ABAP Code to Call the API
Below is the ABAP code to call the /loads
endpoint using the POST method.
REPORT z_cubemaster_api_create_load. DATA: lo_http_client TYPE REF TO if_http_client, lv_url TYPE string, lv_response TYPE string, lv_status TYPE i, lv_token TYPE string VALUE 'YOUR_API_KEY', " Replace with your token lv_json_input TYPE string, lv_json_output TYPE string. START-OF-SELECTION. " Define the API endpoint lv_url = 'https://api.cubemaster.net/loads'. " Endpoint to create a new load " Create HTTP client CALL METHOD cl_http_client=>create_by_url EXPORTING url = lv_url IMPORTING client = lo_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4. IF sy-subrc <> 0. WRITE: / 'Error creating HTTP client'. RETURN. ENDIF. " Set request method (POST) lo_http_client->request->set_method( 'POST' ). " Set headers lo_http_client->request->set_header_field( name = 'TokenID' value = lv_token ). " Add the api token lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ). " Prepare JSON payload for the request body lv_json_input = '{ "Title": "New Mixed Truck Load", "Description": "Hello Web API", "Cargoes": [ { "Name": "ITEM001", "Length": 72, "Width": 30, "Height": 75, "Weight": 1002.45, "OrientationsAllowed": "OrientationsAll", "TurnAllowedOnFloor": false, "Qty": 16, "ColorKnownName": "Brown" }, { "Name": "ITEM002", "Length": 27.31, "Width": 37.5, "Height": 76.67, "Weight": 521.45, "OrientationsAllowed": "OrientationsAll", "TurnAllowedOnFloor": false, "Qty": 28, "ColorKnownName": "Aqua" }, { "Name": "SKU0005", "Length": 27.31, "Width": 9.5, "Height": 75.67, "Weight": 501.45, "OrientationsAllowed": "OrientationsAll", "TurnAllowedOnFloor": true, "Qty": 24, "ColorKnownName": "Beige" }, { "Name": "SKU0005", "Qty": 23 }, { "Name": "SKU0008", "Qty": 34 } ], "Containers": [ { "VehicleType": "Dry", "Name": "53FT-Intermodal", "Length": 630, "Width": 98, "Height": 106, "ColorKnownName": "Blue" } ], "Rules": { "IsWeightLimited": true, "IsSequenceUsed": false, "FillDirection": "FrontToRear", "CalculationType": "MixLoad" } }'. " Replace the JSON payload with the actual structure required by the CubeMaster API " Set the request body lo_http_client->request->set_cdata( lv_json_input ). " Send the request CALL METHOD lo_http_client->send EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 4. IF sy-subrc <> 0. WRITE: / 'Error sending request'. RETURN. ENDIF. " Receive the response CALL METHOD lo_http_client->receive EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 4. IF sy-subrc <> 0. WRITE: / 'Error receiving response'. RETURN. ENDIF. " Get the response status lv_status = lo_http_client->response->get_status( ). " Get the response data lv_response = lo_http_client->response->get_cdata( ). " Display the response WRITE: / 'Status:', lv_status. WRITE: / 'Response:', lv_response. " Parse JSON response (optional) TRY. DATA(lo_json) = /ui2/cl_json=>generate( json = lv_response ). DATA(lt_data) = lo_json->get_data( ). WRITE: / 'Parsed JSON Data:', lt_data. CATCH cx_root INTO DATA(lx_error). WRITE: / 'Error parsing JSON:', lx_error->get_text( ). ENDTRY.
Step 4: Test the Program
Run the program in Transaction Code: SE38 and check the output for the API response.
Use Transaction Code: SE80 to debug the program if necessary.
Test the API Using Postman
Before integrating the API into SAP, you can test it using Postman to ensure it works as expected.
- Open Postman: - Launch Postman and create a new request.
-
Set Request Method:
- Select
POST
as the request method. -
Enter the URL:
- Enter the API endpoint:
https://api.cubemaster.net/loads
. -
Set Headers:
- Add the following headers:
TokenID: YOUR_API_KEY
Content-Type: application/json
-
Set Request Body:
- Go to the
Body
tab, selectraw
, and enter the JSON payload:{ "title": "New Load Plan", "cargoes": [ { "name": "Boxes", "length": 1.2, "width": 1.2, "height": 1.0, "weight": 100, "qty": 50 } ] }
-
Send the Request:
- Click
Send
to execute the request. -
Check the Response:
- Verify the response status code (e.g.,
201 Created
) and the response body.
Step 5: Handle Errors
Check the lv_status
variable for HTTP status codes (e.g., 201 for created, 400 for bad request).
Use a TRY...CATCH
block to handle exceptions during JSON parsing.
Step 6: Deploy and Monitor
- Deploy API integration to SAP production environment.
- Monitor logs and troubleshoot using SAP Cloud Integration tools.