Skip to main content

Actions

The ballerinax/sap package exposes the following clients:

ClientPurpose
ClientHTTP client for SAP APIs with built-in CSRF token authentication.

Client​

HTTP client for SAP APIs with built-in CSRF token authentication.

Configuration​

FieldTypeDefaultDescription
authCredentialsConfig|BearerTokenConfig|OAuth2RefreshTokenGrantConfig|...RequiredAuthentication configuration. Supports Basic Auth (username/password), Bearer Token, or OAuth 2.0 grants.
httpVersionHttpVersionHTTP_2_0HTTP protocol version used by the client.
timeoutdecimal60Maximum time to wait (in seconds) for a response before closing the connection.
retryConfigRetryConfig()Retry configuration for failed requests.
secureSocketClientSecureSocket()SSL/TLS configuration for secure connections.
proxyProxyConfig()Proxy server configuration.
cacheCacheConfig{}HTTP caching configuration.
compressionCompressionCOMPRESSION_AUTOSpecifies the way of handling the Accept-Encoding header.
forwardedstring"disable"Configuration for the Forwarded/X-Forwarded header handling.
poolConfigPoolConfiguration()Connection pool configuration.
validationbooleantrueEnables inbound payload validation provided by the constraint package.

Initializing the client​

import ballerinax/sap;

configurable string hostname = ?;
configurable string username = ?;
configurable string password = ?;

sap:Client sapClient = check new (string `https://${hostname}/sap/opu/odata/sap/API_SALES_ORDER_SRV`, {
auth: {
username,
password
}
});

Operations​

Read operations​

get

Sends an HTTP GET request to the specified SAP API path. Used to retrieve data such as entity collections or individual records.

Parameters:

NameTypeRequiredDescription
pathstringYesResource path relative to the base URL (e.g., "/A_SalesOrder").
headersmap<string|string[]>?NoOptional HTTP headers.
targetTypetypedesc<TargetType>NoExpected return type for data binding.

Returns: targetType|ClientError

Sample code:

json salesOrders = check sapClient->get("/A_SalesOrder");

Sample response:

{
"d": {
"results": [
{
"SalesOrder": "1000000",
"SalesOrderType": "OR",
"SalesOrganization": "1710",
"SoldToParty": "17100001",
"OverallSDProcessStatus": "C"
}
]
}
}
head

Sends an HTTP HEAD request to the specified SAP API path. Returns only headers, useful for checking resource existence or metadata.

Parameters:

NameTypeRequiredDescription
pathstringYesResource path.
headersmap<string|string[]>?NoOptional HTTP headers.

Returns: http:Response|ClientError

Sample code:

http:Response headResponse = check sapClient->head("/A_SalesOrder('1000000')");
int statusCode = headResponse.statusCode;
options

Sends an HTTP OPTIONS request to discover the allowed methods and capabilities of the target SAP API endpoint.

Parameters:

NameTypeRequiredDescription
pathstringYesResource path.
headersmap<string|string[]>?NoOptional HTTP headers.
targetTypetypedesc<TargetType>NoExpected return type.

Returns: targetType|ClientError

Sample code:

http:Response optionsResponse = check sapClient->options("/A_SalesOrder");
string? allowedMethods = check optionsResponse.getHeader("Allow");

Create operations​

post

Sends an HTTP POST request to the specified SAP API path. Used to create new entities. CSRF token is automatically fetched and included.

Parameters:

NameTypeRequiredDescription
pathstringYesResource path (e.g., "/A_SalesOrder").
messageRequestMessageYesThe request payload; can be json, xml, string, byte[], or http:Request.
headersmap<string|string[]>?NoOptional HTTP headers.
mediaTypestring?NoMIME type of the request entity.
targetTypetypedesc<TargetType>NoExpected return type.

Returns: targetType|ClientError

Sample code:

json newOrder = {
SalesOrderType: "OR",
SalesOrganization: "1710",
DistributionChannel: "10",
OrganizationDivision: "00",
SoldToParty: "17100001"
};
json createdOrder = check sapClient->post("/A_SalesOrder", newOrder);

Sample response:

{
"d": {
"SalesOrder": "1000042",
"SalesOrderType": "OR",
"SalesOrganization": "1710",
"DistributionChannel": "10",
"SoldToParty": "17100001"
}
}

Update operations​

put

Sends an HTTP PUT request to the specified SAP API path. Used to fully replace an existing entity. CSRF token is automatically included.

Parameters:

NameTypeRequiredDescription
pathstringYesResource path including the entity key (e.g., "/A_SalesOrder('1000000')").
messageRequestMessageYesThe full replacement payload.
headersmap<string|string[]>?NoOptional HTTP headers.
mediaTypestring?NoMIME type of the request entity.
targetTypetypedesc<TargetType>NoExpected return type.

Returns: targetType|ClientError

Sample code:

json updatedOrder = {
SalesOrderType: "OR",
SalesOrganization: "1710",
DistributionChannel: "10",
OrganizationDivision: "00",
SoldToParty: "17100001",
PurchaseOrderByCustomer: "PO-12345"
};
http:Response putResponse = check sapClient->put("/A_SalesOrder('1000042')", updatedOrder);
patch

Sends an HTTP PATCH request to the specified SAP API path. Used to partially update an existing entity. CSRF token is automatically included.

Parameters:

NameTypeRequiredDescription
pathstringYesResource path including the entity key.
messageRequestMessageYesThe partial update payload containing only the fields to change.
headersmap<string|string[]>?NoOptional HTTP headers.
mediaTypestring?NoMIME type of the request entity.
targetTypetypedesc<TargetType>NoExpected return type.

Returns: targetType|ClientError

Sample code:

json partialUpdate = {
PurchaseOrderByCustomer: "PO-99999"
};
http:Response patchResponse = check sapClient->patch("/A_SalesOrder('1000042')", partialUpdate);

Delete operations​

delete

Sends an HTTP DELETE request to the specified SAP API path. Used to remove an existing entity. CSRF token is automatically included.

Parameters:

NameTypeRequiredDescription
pathstringYesResource path including the entity key (e.g., "/A_SalesOrder('1000042')").
messageRequestMessageNoAn optional request payload.
headersmap<string|string[]>?NoOptional HTTP headers.
mediaTypestring?NoMIME type of the request entity.
targetTypetypedesc<TargetType>NoExpected return type.

Returns: targetType|ClientError

Sample code:

http:Response deleteResponse = check sapClient->delete("/A_SalesOrder('1000042')");

Resource function style​

GET resource accessor

Resource function style for HTTP GET requests. Allows path segments as part of the resource accessor syntax and supports query parameters via the params spread field.

Parameters:

NameTypeRequiredDescription
pathhttp:PathParamType...YesResource path segments (e.g., /A_SalesOrder).
headersmap<string|string[]>?NoOptional HTTP headers.
targetTypetypedesc<TargetType>NoExpected return type.
paramsQueryParamsNoQuery parameters as a record with `string

Returns: targetType|ClientError

Sample code:

json salesOrderList = check sapClient->/A_SalesOrder();

Sample response:

{
"d": {
"results": [
{
"SalesOrder": "1000000",
"SalesOrderType": "OR",
"SalesOrganization": "1710",
"SoldToParty": "17100001"
}
]
}
}
POST resource accessor

Resource function style for HTTP POST requests. Allows path segments as part of the resource accessor syntax. CSRF token is handled automatically.

Parameters:

NameTypeRequiredDescription
pathhttp:PathParamType...YesResource path segments.
messageRequestMessageYesThe request payload.
headersmap<string|string[]>?NoOptional HTTP headers.
mediaTypestring?NoMIME type of the request entity.
targetTypetypedesc<TargetType>NoExpected return type.
paramsQueryParamsNoQuery parameters.

Returns: targetType|ClientError

Sample code:

json newOrder = {
SalesOrderType: "OR",
SalesOrganization: "1710",
DistributionChannel: "10",
OrganizationDivision: "00",
SoldToParty: "17100001"
};
json result = check sapClient->/A_SalesOrder.post(newOrder);

Sample response:

{
"d": {
"SalesOrder": "1000043",
"SalesOrderType": "OR",
"SalesOrganization": "1710",
"SoldToParty": "17100001"
}
}
PATCH resource accessor

Resource function style for HTTP PATCH requests. CSRF token is handled automatically.

Parameters:

NameTypeRequiredDescription
pathhttp:PathParamType...YesResource path segments including the entity key.
messageRequestMessageYesThe partial update payload.
headersmap<string|string[]>?NoOptional HTTP headers.
targetTypetypedesc<TargetType>NoExpected return type.
paramsQueryParamsNoQuery parameters.

Returns: targetType|ClientError

Sample code:

string salesOrderId = "1000042";
http:Response patchResp = check sapClient->/A_SalesOrder(salesOrderId).patch({
PurchaseOrderByCustomer: "PO-UPDATED"
});
DELETE resource accessor

Resource function style for HTTP DELETE requests. CSRF token is handled automatically.

Parameters:

NameTypeRequiredDescription
pathhttp:PathParamType...YesResource path segments including the entity key.
messageRequestMessageNoAn optional request payload.
headersmap<string|string[]>?NoOptional HTTP headers.
targetTypetypedesc<TargetType>NoExpected return type.
paramsQueryParamsNoQuery parameters.

Returns: targetType|ClientError

Sample code:

string salesOrderId = "1000042";
http:Response deleteResp = check sapClient->/A_SalesOrder(salesOrderId).delete();