Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

earmsby's avatar

Filament Widget that gets data from WooCommerce via API

I have set up a connection to a WooCommerce shop's API in my Filament app as follows:

In my .env file:

WOO_STORE_URL="https://digital.subitomusic.com"
WOO_CONSUMER_KEY="ck_XXXXXXXXXXXXXXXXXXXXXXXXXX"
WOO_CONSUMER_SECRET="cs_XXXXXXXXXXXXXXXXXXXXXXXXXX"

In my config/services files:

    'woocommerce' => [
        'url' => env('WOO_STORE_URL'),
        'consumer_key' => env('WOO_CONSUMER_KEY'),
        'consumer_secret' => env('WOO_CONSUMER_SECRET'),
    ],

In my WooCommerceServiceProvider file:

All of this is exactly the same as another filament app except for the different values in the .env file.

I more or less duplicated a widget from the app where the API connection is working just fine. Here is the widget code:

namespace App\Filament\Widgets;

use Automattic\WooCommerce\Client;
use Filament\Widgets\Widget;

class TestWooApi extends Widget
{
    protected string $view = 'filament.widgets.test-woo-api';

    public $products = [];

    public function mount(Client $woocommerceClient): void
    {
        $this->products = $woocommerceClient->get('products');
    }
}

And the blade for the widget:

<x-filament-widgets::widget>
    <x-filament::section>
        {{-- Widget content --}}
        <h2 class="text-xl font-bold">Products Added:</h2>
        <ul class="list-disc list-inside">
            @foreach($products as $product)
                <li>
                    #{{ $product->id }} - {{ $product->name }} ({{ $product->status }}
                </li>
            @endforeach
        </ul>
    </x-filament::section>
</x-filament-widgets::widget>

I'm getting the error: JSON ERROR: Syntax error

Is there a way I can determine if the code is unable to connect to the API or if it is, but there's some error with the data being returned? The error seems so vague and I can't seem to pinpoint the source.

0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! This is a classic scenario when integrating with an external API via Filament, and the "JSON ERROR: Syntax error" typically means something unexpected happened with the API response—either it isn't JSON at all, or it's malformed (or there was an error in the HTTP request, like a 404 or authentication error). Here’s a step-by-step solution you can use to debug and ensure robust data fetching in your widget.


1. Catch and Inspect API Exceptions

Wrap your API call in try/catch so you can log or display detailed error information. The Automattic\WooCommerce\Client will throw exceptions if, for example, it can’t connect or the credentials are wrong.

Update your Widget:


2. Show Errors in the Blade View

Display any errors to help you debug:

<x-filament-widgets::widget>
    <x-filament::section>
        {{-- Widget content --}}
        <h2 class="text-xl font-bold">Products Added:</h2>

        @if($error)
            <div class="text-red-600 font-semibold">Error: {{ $error }}</div>
        @endif

        @if($products)
        <ul class="list-disc list-inside">
            @foreach($products as $product)
                <li>
                    #{{ $product->id ?? '-' }} - {{ $product->name ?? '-' }} ({{ $product->status ?? '-' }})
                </li>
            @endforeach
        </ul>
        @endif
    </x-filament::section>
</x-filament-widgets::widget>

3. Additional Debugging Steps

a. Check for HTTP Issues:

Sometimes an issue isn't being propagated by the Automattic client. Try making a direct HTTP request using Guzzle or even curl to the endpoint with your credentials to see the exact output.

For example, in Tinker:

$wc = new \Automattic\WooCommerce\Client(
    config('services.woocommerce.url'),
    config('services.woocommerce.consumer_key'),
    config('services.woocommerce.consumer_secret'),
    ['version' => 'wc/v3']
);

$response = $wc->get('products');
dd($response);

b. Enable Debug/Logging

Increase logging in your .env:

APP_DEBUG=true
LOG_LEVEL=debug

c. Invalid Certificates / HTTP vs HTTPS

If your WooCommerce store uses HTTPS with a self-signed or invalid certificate, the client might silently fail. You can pass:

[
    'version' => 'wc/v3',
    'verify_ssl' => false, // for testing only!
]

But do not use this in production.


4. Confirm Configuration

  • Ensure your .env is loaded (run php artisan config:clear after changes).
  • Confirm the WooCommerceServiceProvider is registered in your app.

5. Final note on "JSON ERROR: Syntax error"

This error comes from Automattic\WooCommerce\Client when it tries to decode the response as JSON but can't. That typically means one of:

  • The API credentials are incorrect (so it's getting an HTML error page, not JSON).
  • The API endpoint is wrong/returns 404 or something else.
  • The response is empty/invalid.

Logging the response or the exception message will usually tell you exactly what came back from Woo, or what exception occurred.


TL;DR

Replace your API call in the widget with a try/catch to log and show actual errors:

public function mount(Client $woocommerceClient): void
{
    try {
        $this->products = $woocommerceClient->get('products');
    } catch (\Exception $e) {
        $this->error = $e->getMessage();
        \Log::error('WooCommerce API error', ['exception' => $e]);
    }
}

And display $error in your widget view.


By following this approach, you'll quickly be able to see whether:

  • The app is failing to connect at all,
  • The credentials are bad,
  • Or the API is returning something unexpected.

Let me know if you need help with deeper debugging or interpreting the error message!

Glukinho's avatar

I'm getting the error: JSON ERROR: Syntax error

Where is it exactly? In browser window, in browser console, in logs?..

Please or to participate in this conversation.