Laravel MPP: Charge AI Agents via 402 Payment Required | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://www.msaied.com/public) - [Home](https://www.msaied.com/public)
- [Projects](https://www.msaied.com/public/projects)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [About](https://www.msaied.com/public#about)

           [  Contact](https://www.msaied.com/public#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://www.msaied.com/public)
- [ProjectsCase studies](https://www.msaied.com/public/projects)
- [ArticlesEngineering notes](https://www.msaied.com/public/articles)
- [CertificatesCredentials](https://www.msaied.com/public/certificates)
- [AboutHow I work](https://www.msaied.com/public#about)
- [ContactGet in touch](https://www.msaied.com/public#contact)

  [Start a conversation](https://www.msaied.com/public#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://www.msaied.com/public)
2. /
3. [Articles](https://www.msaied.com/public/articles)
4. /
5. [Laravel](https://www.msaied.com/public/articles?category=laravel)
6. /
7. Laravel MPP: Charge AI Agents for API Access with 402 Payment Required

   [Laravel](https://www.msaied.com/public/articles?category=laravel) [AI](https://www.msaied.com/public/articles?category=ai) 

 Laravel MPP: Charge AI Agents for API Access with 402 Payment Required
=======================================================================

 Laravel MPP is a middleware package that implements the Machine Payments Protocol, letting you gate routes behind HTTP 402 challenges so AI agents can pay for API access via Stripe SPTs or Tempo pathUSD.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://www.msaied.com/public#person) Published 7 Jul 2026 · Updated 7 Jul 2026 · 3 min read

ShareCopy linkCopied

 ![Laravel MPP: Charge AI Agents for API Access with 402 Payment Required](https://cdn.msaied.com/386/b58ff453a69cf63b07d792734240afc4.png) 

  On this page +1. [What Is Laravel MPP?](#what-is-laravel-mpp)
2. [Gating a Route](#gating-a-route)
3. [Metered Sessions](#metered-sessions)
4. [Preconditions](#preconditions)
5. [Payment Rails](#payment-rails)
6. [Installation](#installation)
7. [Key Takeaways](#key-takeaways)

 What Is Laravel MPP?
--------------------

[Laravel MPP](https://github.com/square1-io/laravel-mpp) (`square1/laravel-mpp`) is a middleware package that implements the **Machine Payments Protocol (MPP)**. Instead of blocking unauthorized requests with a 401 or 403, it returns an HTTP `402 Payment Required` response containing an HMAC-signed challenge. An AI agent that understands MPP fulfills the challenge over a supported payment rail and retries the request — no human in the loop required.

The package is currently at v1.1.0, MIT licensed, and both MPP and the Stripe SPT APIs it relies on are still in preview.

Gating a Route
--------------

Attaching the `mpp` middleware to any route is all it takes to enable payment enforcement:

```php
Route::get('/resource', MyPaidResource::class)
    ->middleware('mpp:0.50,USD');

```

You can also declare pricing declaratively with the `RequiresPayment` PHP attribute on a controller method and enable automatic enforcement via an environment variable:

```php
#[RequiresPayment(amount: '5.00', currency: 'USD', grants: 10)]
public function report()
{
    // ...
}

Route::get('/report', ReportController::class)->middleware('mpp');

```

Set `MPP_ATTRIBUTES_ENABLED=true` in your `.env` and the middleware reads the attribute automatically.

Metered Sessions
----------------

When `grants` is greater than one, a single payment covers multiple accesses. The successful response includes a `Payment-Session` header:

```php
Payment-Session: id="sess_...", remaining="9", scope="report.basic", expiresAt="..."

```

Subsequent requests replay the session ID instead of paying again:

```bash
curl -si https://your-host/report \
  -H 'Authorization: Payment method="stripe", session="sess_..."'

```

Sessions default to the cache driver but can be persisted to the database:

```ini
MPP_SESSION_DRIVER=database
MPP_SESSION_CACHE_STORE=redis

```

Run `php artisan vendor:publish --tag=mpp-migrations && php artisan migrate` when using the database driver.

Preconditions
-------------

Preconditions are named checks that run *before* the payment gate, so you can reject ineligible requests without asking an agent to pay first. Register them in `config/mpp.php`:

```php
'preconditions' => [
    'checks' => [
        'postexists' => [\App\Mpp\Checks\PostExists::class, 'check'],
    ],
    'global' => ['usernotblocked'],
],

```

A check returns `null` to pass or a `Response` to short-circuit:

```php
public function check(Request $request, PaymentSpec $spec): ?Response
{
    return Post::find($request->route('post'))
        ? null
        : response()->json(['error' => 'No such post.'], 404);
}

```

Attach a precondition per route with the middleware parameter:

```php
Route::get('/posts/{post}', ShowPost::class)
    ->middleware('mpp:1.00,USD,preconditions=postexists');

```

Payment Rails
-------------

The package ships with two built-in rails:

- **Stripe** — uses Shared Payment Tokens (SPTs); configure `STRIPE_SECRET_KEY`, `STRIPE_NETWORK_ID`, and `STRIPE_API_VERSION`.
- **Tempo** — settles in pathUSD on-chain; select it per route with `method=tempo`.

You can accept multiple rails on one route (`methods=stripe|acme`) and register a custom rail by implementing the `Verifier` interface.

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

```bash
composer require square1/laravel-mpp
php artisan vendor:publish --tag=mpp-config

```

Key Takeaways
-------------

- Attach `mpp` middleware to any route to gate it behind an HTTP 402 payment challenge.
- Supports Stripe SPTs and Tempo pathUSD out of the box; custom rails are possible via the `Verifier` interface.
- Metered sessions let one payment cover multiple requests, tracked atomically in cache or database.
- Preconditions let you reject ineligible requests before the payment gate runs.
- Pricing can be declared inline in the middleware string or via the `RequiresPayment` PHP attribute.
- The package is in preview (v1.1.0, MIT); the underlying Stripe SPT API may change.

---

*Source: [Laravel News — Laravel MPP: Charge AI Agents for API Access with 402 Payment Required](https://laravel-news.com/laravel-mpp-charge-ai-agents-for-api-access-with-402-payment-required)*

- [Laravel](https://www.msaied.com/public/articles?search=Laravel)
- [AI Agents](https://www.msaied.com/public/articles?search=AI%20Agents)
- [Middleware](https://www.msaied.com/public/articles?search=Middleware)
- [Payments](https://www.msaied.com/public/articles?search=Payments)
- [Machine Payments Protocol](https://www.msaied.com/public/articles?search=Machine%20Payments%20Protocol)
- [API](https://www.msaied.com/public/articles?search=API)

 Frequently asked questions 
---------------------------

  What payment methods does Laravel MPP support out of the box?Laravel MPP ships with two payment rails: Stripe Shared Payment Tokens (SPTs) and Tempo pathUSD (on-chain). You can also register a custom rail by implementing the package's Verifier interface, and multiple rails can be accepted on a single route.

   How do metered sessions work in Laravel MPP?When a payment specifies a `grants` value greater than one, a single payment covers that many accesses. The server returns a `Payment-Session` header with the session ID and remaining balance. The agent includes that session ID in subsequent requests instead of paying again. Sessions are stored in the cache by default, or in the database for persistence.

   What are preconditions and why would I use them?Preconditions are named checks that run before the payment gate. They let you reject ineligible requests — for example, a missing resource or a blocked user — before an agent is asked to pay, avoiding unnecessary payment challenges for requests that would fail anyway.

   ![Mohamed Said](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp)About the author
----------------

[Mohamed Said](https://www.msaied.com/public#person)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

[About](https://www.msaied.com/public#about) [GitHub ↗](https://github.com/EG-Mohamed) [LinkedIn ↗](https://www.linkedin.com/in/msaiedm/) [WhatsApp ↗](https://wa.me/201094619204) [Email Address ↗](mailto:hello@msaied.com) [My CV ↗](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)  

   [Previous articleLivewire v3 Internals: Morph Markers, JS Hooks, and Alpine Integration](https://www.msaied.com/public/articles/livewire-v3-internals-morph-markers-js-hooks-and-alpine-integration-2) [Next articleLaravel AI SDK: Tool-Calling Agents and Conversation Persistence](https://www.msaied.com/public/articles/laravel-ai-sdk-tool-calling-agents-and-conversation-persistence-2)  

   On this page
-------------

1. [What Is Laravel MPP?](#what-is-laravel-mpp)
2. [Gating a Route](#gating-a-route)
3. [Metered Sessions](#metered-sessions)
4. [Preconditions](#preconditions)
5. [Payment Rails](#payment-rails)
6. [Installation](#installation)
7. [Key Takeaways](#key-takeaways)

 ###  Have a technical challenge?

 Tell me what you’re building. I reply within two working days.

[Start a conversation](https://www.msaied.com/public#contact) 

   Related articles
-----------------

 [ ![](https://cdn.msaied.com/740/cce86edc21eddcbdd2f2454fadaf9c70.png)  · 3 min read### The Pipeline Pattern in Laravel: Custom Pipelines Beyond Middleware

5 Oct 2026 ](https://www.msaied.com/public/articles/the-pipeline-pattern-in-laravel-custom-pipelines-beyond-middleware-1) [ ![](https://cdn.msaied.com/739/2d6897fdcdcf090613f96f72a64b8a78.png)  · 4 min read### MySQL Full-Text Search in Laravel: Indexes, Relevance Scoring, and Boolean Mode

4 Oct 2026 ](https://www.msaied.com/public/articles/mysql-full-text-search-in-laravel-indexes-relevance-scoring-and-boolean-mode) [ ![](https://cdn.msaied.com/738/073696a3fefe18bec825beec5ac658f5.png)  · 4 min read### Laravel Queue Rate-Limited Middleware: Throttling Jobs Without Losing Work

4 Oct 2026 ](https://www.msaied.com/public/articles/laravel-queue-rate-limited-middleware-throttling-jobs-without-losing-work) 

  Have a technical challenge?
----------------------------

Tell me what you’re building. I reply within two working days.

 [Discuss your project ↗](https://www.msaied.com/public#contact) 

  © 2026 Mohamed Said · Built with Laravel, meant to last.Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

 - [Home](https://www.msaied.com/public)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [GitHub](https://github.com/EG-Mohamed)
- [LinkedIn](https://www.linkedin.com/in/msaiedm/)
- [WhatsApp](https://wa.me/201094619204)
- [Email Address](mailto:hello@msaied.com)
- [My CV](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
- [Sitemap](https://www.msaied.com/public/sitemap.xml)
