Search by

spatie / spatie-price-api

spatie

The Price API used at promotional sites for our own products

Package info

github.com/spatie/spatie-price-api

pkg:composer/spatie/spatie-price-api

Fund package maintenance!

spatie

Other

Statistics

Installs: 17 351

Dependents: 1

Suggesters: 0

Stars: 15

Open Issues: 0

1.7.0 2026-10-03 14:27 UTC

This package is auto-updated.

Last update: 2026-10-03 14:28:28 UTC


README

Latest Version on Packagist Tests Total Downloads

This package can retrieve prices from the API at spatie.be. It is used at the promotional sites for our own products. Though it is open source, the package is not intended to be used by third parties.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/spatie-price-api

Usage

You can get a pricing information using the Spatie\PriceApi\SpatiePriceApi::getPriceForPurchasable() method.

Fetching prices in the browser

The price depends on the country of the visitor. When prices are fetched on the server, a page can't be cached at the edge. Instead, you can let the browser of the visitor fetch the price from the spatie.be API.

Add the scripts to your layout:

{{ \Spatie\PriceApi\SpatiePriceApi::scripts() }}

This adds two Alpine data components: spatiePrice($purchasableId) and spatieBundlePrice($bundleId).

<div x-data="spatiePrice(20)" x-init="init()">
    <template x-if="discount.active">
        <p>
            <span x-text="discount.name"></span> ending in
            <span x-text="countdown.days"></span> days
            <span x-text="countdown.hours"></span> hours
            <span x-text="countdown.minutes"></span> minutes
        </p>
    </template>

    <s x-show="discount.active" x-text="priceWithoutDiscount"></s>

    <span x-text="couldFetchPrice ? price : '-'">-</span>
</div>

These properties are available:

  • loaded: whether the request to the API has finished
  • couldFetchPrice: whether a price could be fetched
  • price and priceWithoutDiscount: the formatted price, for example 149 EUR
  • discount: an object with active, percentage, name and expiresAt (a unix timestamp)
  • countdown: an object with days, hours, minutes and seconds until the discount expires, updated every second
  • response: the raw response of the API

Alpine v3 calls init() automatically. On Alpine v2, add x-init="init()". Calling it twice is harmless. Components for the same product share a single request.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.