Setup
To talk to a shop you need two things: the shop's API base URL and a webservice API key. Both go into the client when you create it.
Getting an API key
In the PrestaShop back office:
- Go to Advanced Parameters → Webservice and enable the webservice.
- Add a new key. PrestaShop generates the key string for you.
- Grant the key permissions for the resources you'll use (products, categories, stock, and so on) — and the HTTP methods you need (GET, POST, PUT, DELETE).
- Make sure the key is associated with your shop.
Your base URL is your shop URL followed by /api/, for example
https://your-shop.com/api/.
The webservice uses HTTP Basic auth, with the API key as the username and an empty password. PrestaKit handles this for you, you just supply the key.
Creating the client
- Dependency Injection
- Manual
Register the client once at startup:
using Microsoft.Extensions.DependencyInjection;
builder.Services.AddPrestaShopClient(options =>
{
options.BaseUrl = new Uri("https://your-shop.com/api/");
options.ApiKey = "YOUR_WEBSERVICE_KEY";
});
Then inject IPrestaShopClient anywhere:
public class ProductService(IPrestaShopClient client)
{
public Task<Product> GetAsync(long id) => client.Products.GetAsync(id);
}
The DI registration configures the underlying HttpClient (base address and
authentication) for you, and its lifetime is managed by the container.
Create the client directly. You supply an HttpClient with the base address and
authentication header set:
using System.Net.Http.Headers;
using System.Text;
using PrestaKit;
var http = new HttpClient { BaseAddress = new Uri("https://your-shop.com/api/") };
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes("YOUR_WEBSERVICE_KEY:")));
var client = new PrestaShopClient(http);
In long-running applications, prefer the dependency-injection setup (or supply an
HttpClient whose lifetime you manage) rather than creating a new HttpClient
per call. The manual constructor is convenient for scripts and short-lived
processes.
Options
PrestaShopClientOptions has two properties:
| Property | Type | Description |
|---|---|---|
BaseUrl | Uri | The shop's API base URL, e.g. https://shop.com/api/. |
ApiKey | string | The webservice API key. |
Next: Make your first request.