Czym jest Allegro REST API?
Allegro to największy marketplace w Polsce z ponad 20 milionami aktywnych kupujących. Allegro REST API umożliwia programowy dostęp do wszystkich funkcji sprzedawcy: zarządzania ofertami, zamówieniami, wysyłką, cenami i stanem magazynowym.
Zarządzanie ofertami
Tworzenie, edycja, aktywacja/deaktywacja ofert. Masowe aktualizacje cen i stanów.
Zamówienia i wysyłka
Pobieranie checkout-forms, aktualizacja statusów, generowanie etykiet przez Allegro One.
Cennik i stany
Masowa aktualizacja cen przez PATCH, synchronizacja stanów przez Allegro Price.
Rozliczenia
Historia transakcji, faktury Allegro, zestawienia prowizji.
1. Autoryzacja OAuth 2.0 – Device Flow
Allegro API wymaga OAuth 2.0. Dla skryptów automatycznych (bez UI) używaj Device Flow – użytkownik autoryzuje aplikację jednorazowo przez przeglądarkę, a skrypt operuje na uzyskanym tokenie.
Refresh token: Po autoryzacji zapisz zarówno access_token jak i refresh_token. Access token wygasa po 12h, ale refresh token pozwala uzyskać nowy bez ponownego logowania.
import requests
import base64
CLIENT_ID = "TWOJ_CLIENT_ID"
CLIENT_SECRET = "TWOJ_CLIENT_SECRET"
def pobierz_token_aplikacji() -> str:
"""Device flow – autoryzacja aplikacji (skrypty automatyczne)."""
credentials = base64.b64encode(
f"{CLIENT_ID}:{CLIENT_SECRET}".encode()
).decode()
# Krok 1: Zainicjuj device flow
response = requests.post(
"https://allegro.pl/auth/oauth/device",
headers={
"Authorization": f"Basic {credentials}",
"Content-Type": "application/x-www-form-urlencoded",
},
data={"client_id": CLIENT_ID},
)
data = response.json()
print("Otwórz w przeglądarce:", data["verification_uri_complete"])
print("Kod:", data["user_code"])
# Krok 2: Czekaj na zalogowanie użytkownika
device_code = data["device_code"]
interval = data.get("interval", 5)
import time
while True:
time.sleep(interval)
token_resp = requests.post(
"https://allegro.pl/auth/oauth/token",
headers={"Authorization": f"Basic {credentials}"},
data={
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
"device_code": device_code,
},
)
token_data = token_resp.json()
if "access_token" in token_data:
print("Token uzyskany!")
return token_data["access_token"]
if token_data.get("error") == "authorization_pending":
continue
raise Exception("Błąd autoryzacji: " + str(token_data))2. Pobieranie ofert sprzedawcy
Endpoint GET /sale/offers zwraca oferty z paginacją (max 100 na stronę). Filtruj po statusie: ACTIVE, INACTIVE, ENDED.
def pobierz_oferty(token: str, status: str = "ACTIVE") -> list:
"""Pobiera oferty sprzedającego z paginacją."""
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.allegro.public.v1+json",
}
wszystkie = []
limit = 100
offset = 0
while True:
resp = requests.get(
"https://api.allegro.pl/sale/offers",
headers=headers,
params={"publication.status": status, "limit": limit, "offset": offset},
)
data = resp.json()
oferty = data.get("offers", [])
wszystkie.extend(oferty)
if len(oferty) < limit:
break
offset += limit
print(f"Znaleziono {len(wszystkie)} ofert")
for oferta in wszystkie[:5]:
print(
"ID: " + oferta["id"] +
" | " + oferta["name"][:50] +
" | cena: " + str(oferta.get("sellingMode", {}).get("price", {}).get("amount", "?"))
)
return wszystkie3. Pobieranie zamówień (checkout-forms)
Zamówienia w Allegro API to checkout-forms. Endpoint obsługuje filtrowanie po dacie, statusie i kupującym. Paginacja przez offset.
from datetime import datetime, timedelta
def pobierz_zamowienia_allegro(token: str, days_back: int = 7) -> list:
"""Pobiera zamówienia z ostatnich N dni."""
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.allegro.public.v1+json",
}
date_from = (datetime.utcnow() - timedelta(days=days_back)).strftime(
"%Y-%m-%dT%H:%M:%SZ"
)
wszystkie = []
limit = 100
offset = 0
while True:
resp = requests.get(
"https://api.allegro.pl/order/checkout-forms",
headers=headers,
params={
"lineItems.boughtAt.gte": date_from,
"status": "BOUGHT",
"limit": limit,
"offset": offset,
},
)
data = resp.json()
zamowienia = data.get("checkoutForms", [])
wszystkie.extend(zamowienia)
if len(zamowienia) < limit:
break
offset += limit
print(f"Pobrano {len(wszystkie)} zamówień")
for zam in wszystkie[:3]:
buyer = zam["buyer"]["login"]
total = zam["summary"]["totalToPay"]["amount"]
print(" " + zam["id"] + " | " + buyer + " | " + str(total) + " PLN")
return wszystkie4. Aktualizacja ceny oferty
Ceny aktualizuje się przez PATCH /sale/product-offers/{id}. Metoda PATCH pozwala zmienić tylko wybrane pola bez nadpisywania całej oferty.
def aktualizuj_cene_oferty(token: str, offer_id: str, nowa_cena: float) -> dict:
"""Aktualizuje cenę oferty Allegro przez PATCH /sale/product-offers/{id}."""
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.allegro.public.v1+json",
"Content-Type": "application/vnd.allegro.public.v1+json",
}
payload = {
"sellingMode": {
"price": {
"amount": str(nowa_cena),
"currency": "PLN",
}
}
}
resp = requests.patch(
f"https://api.allegro.pl/sale/product-offers/{offer_id}",
headers=headers,
json=payload,
)
if resp.status_code == 200:
print(f"Cena oferty {offer_id} zaktualizowana na {nowa_cena} PLN")
else:
print(f"Błąd {resp.status_code}: {resp.text}")
return resp.json()Potrzebujesz integracji Allegro na zamówienie?
Budujemy niestandardowe integracje Allegro z ERP, BaseLinker, systemami magazynowymi i hurtowniami. Automatyzujemy wystawianie, aktualizację cen i obsługę zamówień.