PHPackages                             yehia-tarek/laravel-erpnext - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. yehia-tarek/laravel-erpnext

ActiveLibrary[API Development](/categories/api)

yehia-tarek/laravel-erpnext
===========================

A Laravel package for interacting with ERPNext / Frappe REST API

1.0.0(today)02↑2900%MITPHPPHP ^8.1

Since Jun 13Pushed todayCompare

[ Source](https://github.com/yehia-tarek/laravel-erpnext)[ Packagist](https://packagist.org/packages/yehia-tarek/laravel-erpnext)[ RSS](/packages/yehia-tarek-laravel-erpnext/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (3)Versions (2)Used By (0)

Laravel ERPNext
===============

[](#laravel-erpnext)

A Laravel package for interacting with ERPNext / Frappe REST API. Supports token, password, and OAuth 2.0 authentication; fluent query builder; full CRUD; remote method calls; and file uploads.

---

Requirements
------------

[](#requirements)

DependencyVersionPHP^8.1Laravel^10.0 | ^11.0Guzzle^7.5---

Installation
------------

[](#installation)

```
composer require yehia-tarek/laravel-erpnext
```

Publish the config file:

```
php artisan vendor:publish --tag=erpnext-config
```

---

Configuration
-------------

[](#configuration)

Add your ERPNext credentials to `.env`:

```
ERPNEXT_BASE_URL=https://mycompany.erpnext.com

# --- Token Auth (recommended) ---
ERPNEXT_AUTH_METHOD=token
ERPNEXT_API_KEY=your_api_key
ERPNEXT_API_SECRET=your_api_secret

# --- Password Auth ---
# ERPNEXT_AUTH_METHOD=password
# ERPNEXT_USERNAME=admin
# ERPNEXT_PASSWORD=secret

# --- OAuth 2.0 ---
# ERPNEXT_AUTH_METHOD=oauth
# ERPNEXT_ACCESS_TOKEN=your_bearer_token

# --- HTTP Options ---
ERPNEXT_TIMEOUT=30
ERPNEXT_VERIFY_SSL=true
```

### Generating API Keys in ERPNext

[](#generating-api-keys-in-erpnext)

1. Go to **User List** → open a user.
2. Click the **Settings** tab.
3. Expand **API Access** and click **Generate Keys**.
4. Copy the **API Secret** immediately (shown only once).
5. Also note the **API Key** in that section.

---

Quick Start
-----------

[](#quick-start)

```
use YehiaTarek\ERPNext\Facades\ERPNext;

// Verify connection
$user = ERPNext::getLoggedUser(); // e.g. "admin@example.com"
```

---

Authentication
--------------

[](#authentication)

### Token (recommended)

[](#token-recommended)

```
ERPNEXT_AUTH_METHOD=token
ERPNEXT_API_KEY=abc123
ERPNEXT_API_SECRET=xyz789
```

Every request sends `Authorization: token abc123:xyz789`.

### Password (session-based)

[](#password-session-based)

```
ERPNEXT_AUTH_METHOD=password
ERPNEXT_USERNAME=admin
ERPNEXT_PASSWORD=secret
```

The package performs a login on first use and reuses the cookie session.

### OAuth 2.0

[](#oauth-20)

```
ERPNEXT_AUTH_METHOD=oauth
ERPNEXT_ACCESS_TOKEN=your_bearer_token
```

Sends `Authorization: Bearer your_bearer_token`.

---

CRUD Operations
---------------

[](#crud-operations)

### Create a document

[](#create-a-document)

```
$invoice = ERPNext::createDocument('Sales Invoice', [
    'customer'    => 'ACME Corp',
    'items'       => [
        ['item_code' => 'ITEM-001', 'qty' => 2, 'rate' => 150],
    ],
]);

echo $invoice['name']; // SINV-00001
```

### Read a document

[](#read-a-document)

```
$invoice = ERPNext::getDocument('Sales Invoice', 'SINV-00001');
echo $invoice['grand_total'];

// Expand all link fields (returns the full linked document instead of just the name)
$invoice = ERPNext::getDocument('Sales Invoice', 'SINV-00001', expandLinks: true);
echo $invoice['customer']['customer_name']; // expanded Customer doc
```

### Update a document (partial update)

[](#update-a-document-partial-update)

```
$invoice = ERPNext::updateDocument('Sales Invoice', 'SINV-00001', [
    'status' => 'Paid',
]);
```

### Delete a document

[](#delete-a-document)

```
ERPNext::deleteDocument('Sales Invoice', 'SINV-00001'); // true on success
```

---

Query Builder
-------------

[](#query-builder)

The fluent query builder wraps `GET /api/resource/:doctype`.

```
use YehiaTarek\ERPNext\Facades\ERPNext;

$invoices = ERPNext::query('Sales Invoice')
    ->fields(['name', 'customer', 'grand_total', 'status'])
    ->filter('status', '=', 'Paid')
    ->filter('grand_total', '>', 5000)
    ->orderBy('grand_total', 'desc')
    ->limit(25)
    ->get(); // returns array of arrays
```

### Available builder methods

[](#available-builder-methods)

MethodDescription`fields(array)`Fields to fetch`filter(field, op, value)`Add an AND filter`filters(array)`Add multiple AND filters at once`orFilter(field, op, value)`Add an OR filter`expand(array)`Expand link fields inline`orderBy(field, direction)`Sort results`limit(int)`Max records to return`offset(int)`Records to skip`paginate(page, perPage)`Page-based pagination`asList()`Return `List[List]` instead of `List[dict]``debug()`Include executed SQL in response`get()`Execute and return array`first()`Execute, return first item or `null``count()`Count matching documents### Filtering operators

[](#filtering-operators)

```
->filter('status', '=',      'Paid')
->filter('amount', '>',      1000)
->filter('amount', '>=',     500)
->filter('name',   'like',   'SINV-%')
->filter('status', 'in',     ['Paid', 'Unpaid'])
->filter('status', 'not in', ['Cancelled'])
->filter('note',   '!=',     null)
```

### Pagination example

[](#pagination-example)

```
// Page 2, 15 records per page
$records = ERPNext::query('Customer')
    ->fields(['name', 'customer_name', 'territory'])
    ->paginate(page: 2, perPage: 15)
    ->get();
```

---

Remote Method Calls
-------------------

[](#remote-method-calls)

Call any whitelisted Python method on ERPNext.

```
// GET method (read-only)
$user = ERPNext::callGet('frappe.auth.get_logged_user');

// POST method (mutates data)
ERPNext::callPost('frappe.client.submit', [
    'doc' => ['doctype' => 'Sales Invoice', 'name' => 'SINV-00001'],
]);

// Generic (specify verb explicitly)
ERPNext::call('erpnext.accounts.doctype.payment_entry.payment_entry.get_outstanding_reference_documents', [
    'args' => ['party_type' => 'Customer', 'party' => 'CUST-001'],
], 'POST');
```

---

File Uploads
------------

[](#file-uploads)

```
// Upload from a local file path
$file = ERPNext::uploadFile(
    filePath: storage_path('app/invoice.pdf'),
    doctype:  'Sales Invoice',
    docname:  'SINV-00001',
    fieldname: 'attachment',
    isPrivate: true,
);

echo $file['file_url'];

// Upload from raw content (e.g. generated PDF)
$file = ERPNext::uploadFileContent(
    content:  $pdfContent,
    filename: 'report.pdf',
    doctype:  'Sales Invoice',
    docname:  'SINV-00001',
);
```

---

Typed Document Resources
------------------------

[](#typed-document-resources)

Extend `Document` for an Eloquent-style interface per DocType:

```
