This guide covers all methods for making requests to the SAP B1 Service Layer.
There are three ways to access the SAP B1 client:
// 1. Using the Facade (recommended)
use SapB1\Facades\SapB1;
$response = SapB1::get('BusinessPartners');
// 2. Using the helper function
$response = sap_b1()->get('BusinessPartners');
// 3. Using dependency injection
use SapB1\Client\SapB1Client;
class MyController
{
public function index(SapB1Client $client)
{
return $client->get('BusinessPartners');
}
}// Get all records
$response = SapB1::get('BusinessPartners');
// Get with OData query
$response = SapB1::query()
->where('CardType', 'cCustomer')
->top(10)
->get('BusinessPartners');// String key
$response = SapB1::find('BusinessPartners', 'C001');
// Numeric key
$response = SapB1::find('Orders', 123);
// Composite key
$response = SapB1::find('DocumentLines', [
'DocEntry' => 123,
'LineNum' => 0,
]);$response = SapB1::create('BusinessPartners', [
'CardCode' => 'C00001',
'CardName' => 'New Customer',
'CardType' => 'cCustomer',
'GroupCode' => 100,
]);$response = SapB1::update('BusinessPartners', 'C001', [
'CardName' => 'Updated Name',
'Phone1' => '+90 555 123 4567',
]);$response = SapB1::delete('BusinessPartners', 'C001');if (SapB1::exists('Items', 'A00001')) {
// Item exists
}$count = SapB1::query()
->where('CardType', 'cCustomer')
->count('BusinessPartners');
echo "Total customers: {$count}";Call SAP B1 document actions:
// Close an order
$response = SapB1::action('Orders', 123, 'Close');
// Cancel a document
$response = SapB1::action('Orders', 123, 'Cancel');
// Action with parameters
$response = SapB1::action('Quotations', 456, 'CreateSalesOrder', [
'DeliveryDate' => '2026-01-20',
]);For advanced use cases:
// POST
$response = SapB1::post('Orders', $data);
// PUT (full replacement)
$response = SapB1::put('BusinessPartners(\'C001\')', $fullData);
// PATCH (partial update)
$response = SapB1::patch('BusinessPartners(\'C001\')', $partialData);
// DELETE without key formatting
$response = SapB1::rawDelete('Orders(123)');$response = SapB1::get('BusinessPartners');
while ($response !== null) {
foreach ($response->value() as $partner) {
// Process partner
}
$response = SapB1::nextPage($response);
}foreach (SapB1::paginate('BusinessPartners') as $response) {
foreach ($response->value() as $partner) {
// Process partner
}
}$response = SapB1::query()
->page(1, 20) // Page 1, 20 items per page
->get('BusinessPartners');
// Or use skip/top
$response = SapB1::query()
->skip(40)
->top(20)
->get('BusinessPartners');For a more expressive syntax:
$client = SapB1::service('BusinessPartners');
// Find
$partner = $client->find('C001')->entity();
// Query
$customers = $client
->select('CardCode', 'CardName')
->where('CardType', 'cCustomer')
->top(10)
->get()
->value();
// Create
$response = $client->create([
'CardCode' => 'C00001',
'CardName' => 'New Customer',
]);
// Update
$response = $client->update('C001', [
'CardName' => 'Updated Name',
]);
// Delete
$response = $client->delete('C001');SAP B1 supports both OData v3 (v1 endpoint) and OData v4 (v2 endpoint):
// Use OData v4 (Service Layer v2)
$response = SapB1::useODataV4()->get('BusinessPartners');
// Use OData v3 (Service Layer v1) - default
$response = SapB1::useODataV3()->get('BusinessPartners');
// Configure in .env
// SAP_B1_ODATA_VERSION=v2// Check session validity
if (SapB1::hasValidSession()) {
// Session is active
}
// Manually refresh session
SapB1::refreshSession();
// Logout
SapB1::logout();// Disable automatic session refresh on 401
$response = SapB1::withoutAutoRefresh()->get('Orders');Configured globally in config/sap-b1.php:
'http' => [
'timeout' => 30, // Request timeout
'connect_timeout' => 10, // Connection timeout
],'http' => [
'retry' => [
'times' => 3,
'sleep' => 1000,
'when' => [429, 500, 502, 503, 504],
],
],- OData Query Builder - Build complex queries
- Working with Responses - Handle response data
- Batch Operations - Execute multiple operations