واجهة مسارك البرمجية (v1)
كل ما يحتاجه مطوّر التاجر أو منصة التجميع لإنشاء الشحنات وطباعة البوالص ومتابعة الحالة من نظامه مباشرة. الاستجابات JSON داخل غلاف { data } والأخطاء داخل { error: { code, message, message_ar, details } }. المبالغ كلها بالقرش (100 قرش = جنيه واحد)، والأوقات بصيغة ISO 8601 بالتوقيت العالمي.
العنوان الأساسي: https://api.masarakhub.com/v1/api
المصادقة
أرسل المفتاح في الهيدر X-Api-Key · صيغة المفتاح: mk_live_<8 prefix><32 secret>
كيف تحصل على مفتاح
بوابة التاجر → الإعدادات → مفاتيح API (صاحب حساب التاجر)، أو تطلبه من شركة الشحن التي تُصدره لحسابك عندها. يُعرض المفتاح مرة واحدة ويُخزَّن مُجزَّأً.
افتح مفاتيح API في بوابة التاجر
الصلاحيات
لكل مفتاح صلاحيات محدّدة تُختار عند إنشائه. المسار الذي يحتاج صلاحية لا يملكها المفتاح يردّ بـ 403 API_KEY_SCOPE.
| الصلاحية | المعنى |
|---|---|
quotes | تسعير شحنة قبل إنشائها (POST /api/quotes). |
shipments:create | إنشاء شحنة باسم التاجر. |
shipments:read | قراءة شحنات التاجر وتفاصيلها. |
labels:read | تحميل بوليصة الشحنة (PDF). |
tracking:read | قراءة حالة الشحنة وتاريخها وموعد الوصول المتوقع. |
مثال سريع
curl -X POST https://api.masarakhub.com/v1/api/shipments \
-H "X-Api-Key: mk_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Idempotency-Key: 6f1c2d7e-order-10042" \
-H "Content-Type: application/json" \
-d '{
"external_reference": "SHOP-10042",
"consignee_name": "Mona Adel",
"consignee_phone": "01012345678",
"consignee_address": "12 Abbas El Akkad St, Nasr City",
"consignee_city": "Nasr City",
"governorate_code": "CAIRO",
"city_code": "CAIRO__NASR_CITY",
"cod_amount": 35000,
"weight_grams": 1200
}'المسارات
POSThttps://api.masarakhub.com/v1/api/shipments
إنشاء شحنة · الصلاحية المطلوبة: shipments:create · حد الطلبات: كتابة — 60 طلبًا في الدقيقة
ينشئ شحنة واحدة للتاجر صاحب المفتاح بنفس التحقق والتسعير اللي في بوابة التاجر. الهيدر Idempotency-Key إلزامي: إعادة إرسال نفس الطلب بنفس المفتاح ترجّع نفس الرد ونفس رقم البوليصة ولا تنشئ شحنة ثانية. أوردر الدفع عند الاستلام (cod_amount > 0) يُشحن فورًا؛ الأوردر المدفوع مسبقًا يُشحن لما تقول payment_status PAID، وبـ UNPAID يُرفض بـ 409 SHIPMENT_HELD من غير إنشاء أي شيء لحد ما المتجر يأكد الدفع. لو شركة الشحن موقّفة استقبال أوردرات التكاملات تُرجع 409 SHIPMENT_HELD بسبب CARRIER_DISABLED.
هيدر إلزامي: Idempotency-Key — الطلب بدونه يُرفض بـ 400 IDEMPOTENCY_KEY_REQUIRED؛ التفاصيل في قسم عدم التكرار أدناه.
جسم الطلب
| الحقل | النوع | مطلوب | الوصف |
|---|---|---|---|
consignee_name | string | نعم | اسم المستلم. (2 إلى 120 حرف) |
consignee_phone | string | نعم | موبايل المستلم المصري. (01XXXXXXXXX) |
consignee_address | string | نعم | عنوان التسليم. (5 إلى 500 حرف) |
consignee_city | string | نعم | المدينة أو المنطقة كما تُعرض. (2 إلى 80 حرف) |
governorate_code | string | لا | كود المحافظة من قائمة مسارك؛ يجعل التوجيه والتسعير مؤكدين. |
city_code | string | لا | كود المدينة داخل المحافظة (مع governorate_code). |
consignee_latitude | number | لا | موقع التسليم على الخريطة (مع consignee_longitude، داخل مصر). |
consignee_longitude | number | لا | موقع التسليم على الخريطة (مع consignee_latitude). |
cod_amount | integer | نعم | المبلغ المُحصَّل عند التسليم بالقرش؛ 0 لشحنة مدفوعة مسبقًا. (0 إلى 50,000,000 قرش) |
declared_value | integer | لا | قيمة البضاعة بالقرش (الافتراضي = cod_amount). |
weight_grams | integer | لا | الوزن بالجرام. (0 إلى 500,000) |
priority | enum | لا | مستوى الخدمة (الافتراضي STANDARD). STANDARD | EXPRESS | SAME_DAY |
notes | string | لا | ملاحظة للمندوب. (حتى 500 حرف) |
external_reference | string | لا | رقم الأوردر عندك. يُحفظ على الشحنة، تبحث به، ولا يُقبل مرتين لنفس التاجر (409 EXTERNAL_REFERENCE_EXISTS). (حتى 64 حرف) |
origin_branch_id | uuid | لا | الفرع اللي تبدأ منه الشحنة. من غيره: آخر فرع استخدمه التاجر، وإلا الفرع الرئيسي للشركة. |
payment_status | enum | لا | كلمة المتجر عن الدفع لأوردر مدفوع مسبقًا. من غيرها يُعامل كمدفوع. PAID | UNPAID |
الاستجابة · 201
| الحقل | النوع | موجود دائمًا | الوصف |
|---|---|---|---|
shipment_id | uuid | نعم | معرّف الشحنة في مسارك. |
awb | string | نعم | رقم البوليصة. |
status | string | نعم | الحالة (CREATED عند الإنشاء). |
external_reference | string | لا | مرجعك كما أُرسل. |
tracking_url | string | نعم | رابط التتبع للمستلم. |
label_url | string | نعم | مسار بوليصة الشحنة: /v1/api/shipments/:id/label.pdf (يحتاج labels:read). |
estimate | object | نعم | الرسوم كما حُسبت على قائمة أسعار التاجر: delivery_fee_piastres، fee_breakdown، return_fee_piastres. |
delivery_deadline | string | لا | الموعد الموعود (ISO). |
أخطاء خاصة بهذا المسار (إضافة إلى الأخطاء المشتركة أدناه)
| الرمز | الكود | الوصف |
|---|---|---|
| 400 | IDEMPOTENCY_KEY_REQUIRED | POST /api/shipments بدون هيدر Idempotency-Key. |
| 422 | IDEMPOTENCY_KEY_CONFLICT | نفس Idempotency-Key مع جسم طلب مختلف. |
| 409 | IDEMPOTENCY_KEY_PROCESSING | طلب بنفس المفتاح قيد التنفيذ؛ أعد المحاولة بعد قليل وستقرأ الرد المخزَّن. |
| 409 | SHIPMENT_HELD | لم يُنشأ شيء: details.hold_reason هو PAYMENT_POLICY (أوردر مدفوع مسبقًا لم يُدفع بعد) أو CARRIER_DISABLED (الشركة أوقفت أوردرات التكاملات). |
| 409 | EXTERNAL_REFERENCE_EXISTS | شحنة بنفس external_reference موجودة لهذا التاجر؛ details تحمل shipment_id وawb. |
| 400 | INVALID_BRANCH | origin_branch_id ليس فرعًا نشطًا للشركة. |
| 409 | NO_ORIGIN_BRANCH | لا فرع نشط للشركة لتبدأ منه الشحنة. |
| 400 | SAME_DAY_CUTOFF_PASSED | SAME_DAY بعد موعد قطع الفرع. |
| 422 | WEIGHT_OVER_LIMIT | الوزن فوق حد قائمة الأسعار. |
| 400 | LOCATION_OUTSIDE_EGYPT | موقع التسليم خارج مصر. |
GEThttps://api.masarakhub.com/v1/api/shipments
قائمة الشحنات · الصلاحية المطلوبة: shipments:read · حد الطلبات: قراءة — 300 طلب في الدقيقة
شحنات التاجر صاحب المفتاح، الأحدث أولًا، بصفحات (cursor). ابحث بمرجعك أو برقم البوليصة أو بالحالة.
معاملات الاستعلام
| الحقل | النوع | مطلوب | الوصف |
|---|---|---|---|
external_reference | string | لا | مرجعك بالضبط. |
awb | string | لا | رقم البوليصة بالضبط. |
status | string | لا | حالة الشحنة (مثل CREATED، OUT_FOR_DELIVERY، DELIVERED). |
limit | integer | لا | حجم الصفحة (الافتراضي 50، الأقصى 200). |
cursor | uuid | لا | meta.next_cursor من الصفحة السابقة. |
الاستجابة · 200
| الحقل | النوع | موجود دائمًا | الوصف |
|---|---|---|---|
items | array | نعم | id، awb، external_reference، status، consignee_name، consignee_city، cod_amount، delivery_fee_piastres، priority، tracking_url، label_url، created_at، updated_at، delivery_deadline. |
meta | object | نعم | next_cursor، has_more، limit، count. |
أخطاء خاصة بهذا المسار (إضافة إلى الأخطاء المشتركة أدناه)
| الرمز | الكود | الوصف |
|---|---|---|
| 400 | VALIDATION_FAILED | حقل غير صالح؛ details[] تسرد كل حقل برسالته بالعربية والإنجليزية. |
GEThttps://api.masarakhub.com/v1/api/shipments/:id
تفاصيل شحنة · الصلاحية المطلوبة: shipments:read · حد الطلبات: قراءة — 300 طلب في الدقيقة
ما يراه التاجر في بوابته: المستلم، المبالغ والرسوم (financials)، محاولات التسليم ودليلها لو الشركة تشاركه، وسجل الحالات (الفاعل كنوع، أبدًا لا اسم موظف).
لا جسم ولا معاملات استعلام؛ معرّف الشحنة في المسار.
الاستجابة · 200
| الحقل | النوع | موجود دائمًا | الوصف |
|---|---|---|---|
id | uuid | نعم | معرّف الشحنة. |
awb | string | نعم | رقم البوليصة. |
status | string | نعم | الحالة الحالية. |
external_reference | string | لا | مرجعك. |
financials | object | نعم | المتوقع والمحصَّل والرسوم وما يصل للتاجر — يحسبها الخادم فقط. |
attempts | array | نعم | محاولات التسليم بترتيبها. |
status_history | array | نعم | from_status، to_status، set_at، set_by_actor، attempt_reason، reason. |
أخطاء خاصة بهذا المسار (إضافة إلى الأخطاء المشتركة أدناه)
| الرمز | الكود | الوصف |
|---|---|---|
| 404 | SHIPMENT_NOT_FOUND | لا شحنة بهذا المعرّف على حساب التاجر صاحب المفتاح. |
| 400 | INVALID_ID | معرّف في المسار ليس UUID. |
GEThttps://api.masarakhub.com/v1/api/shipments/:id/label.pdf
بوليصة الشحنة (PDF) · الصلاحية المطلوبة: labels:read · حد الطلبات: قراءة — 300 طلب في الدقيقة
نفس PDF البوليصة اللي يطبعها التاجر من بوابته، بالبايت. شحنة ملغاة لا بوليصة لها (409 SHIPMENT_CANCELLED).
لا جسم ولا معاملات استعلام؛ معرّف الشحنة في المسار.
الاستجابة · 200
| الحقل | النوع | موجود دائمًا | الوصف |
|---|---|---|---|
application/pdf | string | نعم | جسم الرد هو ملف PDF؛ Content-Disposition: inline; filename="awb-<AWB>.pdf". |
أخطاء خاصة بهذا المسار (إضافة إلى الأخطاء المشتركة أدناه)
| الرمز | الكود | الوصف |
|---|---|---|
| 404 | SHIPMENT_NOT_FOUND | لا شحنة بهذا المعرّف على حساب التاجر صاحب المفتاح. |
| 409 | SHIPMENT_CANCELLED | شحنة ملغاة لا بوليصة لها. |
| 400 | INVALID_ID | معرّف في المسار ليس UUID. |
GEThttps://api.masarakhub.com/v1/api/shipments/:id/tracking
تتبع شحنة · الصلاحية المطلوبة: tracking:read · حد الطلبات: قراءة — 300 طلب في الدقيقة
الحالة بعنوانها بالعربية والإنجليزية، سجل الحالات كما يراه التاجر، نافذة الوصول المتوقع أثناء الخروج للتسليم، ورابط التتبع للمستلم. تغيّرات الحالة تصلك أيضًا عبر الـ webhooks فلا داعي للاستطلاع المتكرر.
لا جسم ولا معاملات استعلام؛ معرّف الشحنة في المسار.
الاستجابة · 200
| الحقل | النوع | موجود دائمًا | الوصف |
|---|---|---|---|
shipment_id | uuid | نعم | معرّف الشحنة. |
awb | string | نعم | رقم البوليصة. |
status | object | نعم | code، label_en، label_ar. |
status_history | array | نعم | كل تغيّر حالة بوقته وفاعله كنوع. |
eta_window | object | لا | estimate، from، to (ISO) أثناء الخروج للتسليم، وإلا null. |
tracking_url | string | نعم | رابط التتبع للمستلم. |
أخطاء خاصة بهذا المسار (إضافة إلى الأخطاء المشتركة أدناه)
| الرمز | الكود | الوصف |
|---|---|---|
| 404 | SHIPMENT_NOT_FOUND | لا شحنة بهذا المعرّف على حساب التاجر صاحب المفتاح. |
| 400 | INVALID_ID | معرّف في المسار ليس UUID. |
POSThttps://api.masarakhub.com/v1/api/quotes
تسعير شحنة · الصلاحية المطلوبة: quotes · حد الطلبات: كتابة — 60 طلبًا في الدقيقة
الرسوم ومواعيد التسليم الموعودة لكل مستوى خدمة على قائمة أسعار التاجر، صالحة ساعة. مستوى الخدمة الذي لا تُسعّره القائمة لا يظهر؛ SAME_DAY يختفي بعد موعد القطع.
جسم الطلب
| الحقل | النوع | مطلوب | الوصف |
|---|---|---|---|
destination_city | string | نعم | مدينة التسليم. (2 إلى 80 حرف) |
destination_governorate_code | string | لا | كود المحافظة. |
destination_city_code | string | لا | كود المدينة. |
destination_address | string | نعم | عنوان التسليم. (3 إلى 500 حرف) |
weight_grams | integer | نعم | الوزن بالجرام. (1 أو أكثر) |
cod_amount | integer | نعم | المبلغ المُحصَّل بالقرش (0 لمدفوع مسبقًا). |
الاستجابة · 200
| الحقل | النوع | موجود دائمًا | الوصف |
|---|---|---|---|
quote_id | uuid | نعم | معرّف التسعيرة. |
expires_at | string | نعم | متى تنتهي صلاحيتها (ISO). |
options | array | نعم | priority، fee_piastres، promised_by لكل مستوى خدمة متاح. |
أخطاء خاصة بهذا المسار (إضافة إلى الأخطاء المشتركة أدناه)
| الرمز | الكود | الوصف |
|---|---|---|
| 400 | VALIDATION_FAILED | حقل غير صالح؛ details[] تسرد كل حقل برسالته بالعربية والإنجليزية. |
أكواد الأخطاء
كل الأكواد التي قد تقابلها مع رمز HTTP لكل منها. أكواد المصادقة والصلاحيات وحالة الحساب وحدود الطلبات قد تأتي من أي مسار؛ وكل مسار أعلاه يسرد ما يخصه.
| الرمز | الكود | الوصف |
|---|---|---|
| 401 | API_KEY_INVALID | الهيدر X-Api-Key ناقص أو المفتاح غير معروف. |
| 401 | API_KEY_REVOKED | المفتاح أُلغي؛ أصدر مفتاحًا جديدًا من بوابة التاجر. |
| 403 | API_KEY_SCOPE | المفتاح لا يحمل الصلاحية المطلوبة؛ details.required تسمّيها. |
| 403 | CLIENT_INACTIVE | حساب التاجر صاحب المفتاح غير نشط. |
| 403 | ORG_READONLY | شركة الشحن موقوفة؛ القراءة فقط. |
| 402 | SUBSCRIPTION_REQUIRED | اشتراك شركة الشحن منتهٍ؛ الكتابة متوقفة حتى تدفع. |
| 400 | VALIDATION_FAILED | حقل غير صالح؛ details[] تسرد كل حقل برسالته بالعربية والإنجليزية. |
| 400 | LOCATION_OUTSIDE_EGYPT | موقع التسليم خارج مصر. |
| 400 | INVALID_ID | معرّف في المسار ليس UUID. |
| 400 | IDEMPOTENCY_KEY_REQUIRED | POST /api/shipments بدون هيدر Idempotency-Key. |
| 400 | INVALID_IDEMPOTENCY_KEY | Idempotency-Key أطول من 128 حرفًا. |
| 422 | IDEMPOTENCY_KEY_CONFLICT | نفس Idempotency-Key مع جسم طلب مختلف. |
| 409 | IDEMPOTENCY_KEY_PROCESSING | طلب بنفس المفتاح قيد التنفيذ؛ أعد المحاولة بعد قليل وستقرأ الرد المخزَّن. |
| 409 | SHIPMENT_HELD | لم يُنشأ شيء: details.hold_reason هو PAYMENT_POLICY (أوردر مدفوع مسبقًا لم يُدفع بعد) أو CARRIER_DISABLED (الشركة أوقفت أوردرات التكاملات). |
| 409 | EXTERNAL_REFERENCE_EXISTS | شحنة بنفس external_reference موجودة لهذا التاجر؛ details تحمل shipment_id وawb. |
| 400 | INVALID_BRANCH | origin_branch_id ليس فرعًا نشطًا للشركة. |
| 409 | NO_ORIGIN_BRANCH | لا فرع نشط للشركة لتبدأ منه الشحنة. |
| 400 | SAME_DAY_CUTOFF_PASSED | SAME_DAY بعد موعد قطع الفرع. |
| 422 | WEIGHT_OVER_LIMIT | الوزن فوق حد قائمة الأسعار. |
| 404 | SHIPMENT_NOT_FOUND | لا شحنة بهذا المعرّف على حساب التاجر صاحب المفتاح. |
| 409 | SHIPMENT_CANCELLED | شحنة ملغاة لا بوليصة لها. |
| 429 | RATE_LIMITED | تجاوزت حد المفتاح؛ Retry-After يقول بعد كم ثانية. |
عدم التكرار (Idempotency)
الهيدر: Idempotency-Key
مفتاح من اختيارك (حتى 128 حرفًا) لكل إنشاء شحنة. نفس المفتاح ونفس الجسم خلال 24 ساعة يرجّعان الرد الأول نفسه (نفس رقم البوليصة). نفس المفتاح مع جسم مختلف: 422. المفاتيح معزولة لكل تاجر.
الـ Webhooks
تصلك تغيّرات الحالة إلى خادمك دون استطلاع. أنشئ اشتراكًا من بوابة التاجر (ويب هوك للمطورين) بعنوان HTTPS عام والأحداث التي تريدها؛ يظهر سرّ التوقيع مرة واحدة. كل تسليم طلب POST بجسم JSON، ومع التوقيع تحمل كل رسالة هيدرات التعريف التالية (التسلسل رقم متزايد للترتيب وإسقاط المكرّر):
X-Masarak-Event-Id · X-Masarak-Event-Type · X-Masarak-Event-Sequence
التحقّق من التوقيع
تحمل كل رسالة هيدر توقيع. احسب الخوارزمية أدناه بسرّ الاشتراك على النص الموقَّع وقارن النتيجة بـ v1 مقارنةً ثابتة الوقت، وارفض ما يبعد طابعه الزمني t عن وقتك أكثر من مدة التسامح. اعتمد على الجسم الخام كما وصل، لا على ما بعد التحليل.
الهيدر: X-Masarak-Signature
صيغة الهيدر: t=<unix seconds>,v1=<hex HMAC-SHA256>
الخوارزمية: HMAC-SHA256 · النص الموقَّع: <t>.<raw request body> · التسامح الزمني: 300 ثانية
الأحداث
| الحدث | المعنى |
|---|---|
shipment.created | شحنة جديدة أُنشئت. |
shipment.status_changed | تغيّرت حالة شحنة (data: shipment_id، awb، from_status، to_status). |
shipment.assigned_to_agent | سُلّمت الشحنة لشريك توصيل. |
shipment.damaged | سُجّلت الشحنة كمتضررة. |
shipment.returned | أُعيدت الشحنة للتاجر. |
cod.collected | حُصّل مبلغ التسليم. |
cod.cashed_in | سلّم المندوب المبلغ للفرع (صار متاحًا للتاجر). |
order.created | أُنشئ أوردر (غلاف الشحنات). |
pickup.requested | طُلب استلام من التاجر. |
pickup.assigned | عُيّن مندوب للاستلام. |
pickup.completed | اكتمل الاستلام. |
pickup.cancelled | أُلغي الاستلام. |
pickup.handoff_received | استلم الفرع شحنات سلّمها التاجر بنفسه. |
pickup.info_responded | ردّ التاجر على طلب معلومات عن الاستلام. |
مثال على الرسالة
حقول جسم الرسالة: event_id, sequence, event_type, occurred_at, aggregate, client_id, data
{
"event_id": "evt_01J9X3V5K8Q2R7T4W6Y8Z0A1B2",
"sequence": 48213,
"event_type": "shipment.status_changed",
"occurred_at": "2026-11-14T09:41:07.000Z",
"aggregate": { "type": "shipment", "id": "0d2f6c0e-7a51-4c3a-9b1e-5f6a7b8c9d0e" },
"client_id": "7c1a2b3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d",
"data": {
"shipment_id": "0d2f6c0e-7a51-4c3a-9b1e-5f6a7b8c9d0e",
"awb": "MK-261114-000123",
"external_reference": "SHOP-10042",
"from_status": "OUT_FOR_DELIVERY",
"to_status": "DELIVERED",
"set_at": "2026-11-14T09:41:07.000Z"
}
}مثال التحقّق (Node.js)
import { createHmac, timingSafeEqual } from 'node:crypto';
// rawBody: the request body as received, before any JSON parsing.
export function verifyMasarakSignature(rawBody, signatureHeader, signingSecret, toleranceSeconds = 300) {
const parts = Object.fromEntries(signatureHeader.split(',').map((kv) => kv.split('=')));
const t = Number(parts.t);
if (!Number.isFinite(t) || Math.abs(Date.now() / 1000 - t) > toleranceSeconds) return false;
const expected = createHmac('sha256', signingSecret).update(`${t}.${rawBody}`).digest('hex');
const given = Buffer.from(parts.v1 ?? '', 'hex');
const wanted = Buffer.from(expected, 'hex');
return given.length === wanted.length && timingSafeEqual(given, wanted);
}
// Express: app.post('/webhooks/masarak', express.raw({ type: 'application/json' }), (req, res) => {
// if (!verifyMasarakSignature(req.body.toString('utf8'), req.header('X-Masarak-Signature') ?? '', process.env.MASARAK_WEBHOOK_SECRET)) {
// return res.status(401).end();
// }
// const event = JSON.parse(req.body.toString('utf8'));
// // handle event.event_type … then answer 2xx quickly.
// res.status(204).end();
// });رد بـ 2xx خلال 10 ثوانٍ. غير ذلك تُعاد المحاولة بسلّم زمني متباعد؛ بعد فشل متواصل يُوقَف الاشتراك ويظهر ذلك في بوابة التاجر.
حدود الطلبات
لكل مفتاح، لكل مسار، في الدقيقة: 60 للكتابة (إنشاء شحنة، تسعير) و300 للقراءة. عند التجاوز: 429 RATE_LIMITED مع هيدر Retry-After بالثواني.
بيئة الاختبار
لا يوجد خادم تجريبي منفصل: اطلب من شركة الشحن حساب تاجر تجريبيًا وأصدر له مفتاحًا؛ الشحنات التجريبية تُلغى من البوابة.
الإصدارات
كل المسارات تحت /v1. التغييرات إضافية فقط: حقول جديدة قد تظهر، ولا يُحذف حقل أو مسار ولا يتغيّر معناه دون نسخة جديدة.
The Masarak API (v1)
Everything a merchant’s developer or an aggregator needs to create shipments, print labels and follow status from their own system. Responses are JSON in a { data } envelope; errors come as { error: { code, message, message_ar, details } }. Amounts are in piastres (100 piastres = 1 pound); times are ISO 8601 in UTC.
Base URL: https://api.masarakhub.com/v1/api
Authentication
Send the key in the header X-Api-Key · Key format: mk_live_<8 prefix><32 secret>
How to get a key
Merchant portal → Settings → API keys (the merchant owner), or ask the shipping company to issue one for your account there. The key is shown once and stored hashed.
Open API keys in the merchant portal
Scopes
Each key carries the scopes chosen on creation. A route that needs a scope the key lacks answers 403 API_KEY_SCOPE.
| Scope | Meaning |
|---|---|
quotes | Price a parcel before creating it (POST /api/quotes). |
shipments:create | Create a shipment for the merchant. |
shipments:read | Read the merchant's shipments and their details. |
labels:read | Download the AWB label (PDF). |
tracking:read | Read a shipment's status, timeline and ETA window. |
Quick example
curl -X POST https://api.masarakhub.com/v1/api/shipments \
-H "X-Api-Key: mk_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Idempotency-Key: 6f1c2d7e-order-10042" \
-H "Content-Type: application/json" \
-d '{
"external_reference": "SHOP-10042",
"consignee_name": "Mona Adel",
"consignee_phone": "01012345678",
"consignee_address": "12 Abbas El Akkad St, Nasr City",
"consignee_city": "Nasr City",
"governorate_code": "CAIRO",
"city_code": "CAIRO__NASR_CITY",
"cod_amount": 35000,
"weight_grams": 1200
}'Routes
POSThttps://api.masarakhub.com/v1/api/shipments
Create a shipment · Required scope: shipments:create · Rate limit: writes — 60 requests a minute
Creates one shipment for the key's merchant with the same validation and pricing as the merchant portal. The Idempotency-Key header is required: resending the same request with the same key returns the same answer and the same AWB, never a second shipment. A cash-on-delivery order (cod_amount > 0) ships at once; a prepaid order ships when you say payment_status PAID, and with UNPAID it is refused with 409 SHIPMENT_HELD and nothing is created until the store confirms payment. When the shipping company has switched integration orders off, the answer is 409 SHIPMENT_HELD with CARRIER_DISABLED.
Required header: Idempotency-Key — a call without it is refused with 400 IDEMPOTENCY_KEY_REQUIRED; see Idempotency below.
Request body
| Field | Type | Required | Description |
|---|---|---|---|
consignee_name | string | yes | The recipient's name. (2 to 120 characters) |
consignee_phone | string | yes | The recipient's Egyptian mobile. (01XXXXXXXXX) |
consignee_address | string | yes | The delivery address. (5 to 500 characters) |
consignee_city | string | yes | The city or area, as displayed. (2 to 80 characters) |
governorate_code | string | no | The governorate code from Masarak's list; makes routing and pricing exact. |
city_code | string | no | The city code inside that governorate (with governorate_code). |
consignee_latitude | number | no | The delivery pin (with consignee_longitude, inside Egypt). |
consignee_longitude | number | no | The delivery pin (with consignee_latitude). |
cod_amount | integer | yes | Cash to collect on delivery, in piastres; 0 for a prepaid parcel. (0 to 50,000,000 piastres) |
declared_value | integer | no | The goods value in piastres (defaults to cod_amount). |
weight_grams | integer | no | Weight in grams. (0 to 500,000) |
priority | enum | no | The service level (default STANDARD). STANDARD | EXPRESS | SAME_DAY |
notes | string | no | A note for the courier. (up to 500 characters) |
external_reference | string | no | Your own order number. Kept on the shipment, searchable, and refused a second time for the same merchant (409 EXTERNAL_REFERENCE_EXISTS). (up to 64 characters) |
origin_branch_id | uuid | no | The branch the parcel starts from. Omitted: the merchant's last used branch, else the company's hub. |
payment_status | enum | no | The store's word on payment for a prepaid order. Omitted: treated as paid. PAID | UNPAID |
Response · 201
| Field | Type | Always present | Description |
|---|---|---|---|
shipment_id | uuid | yes | The shipment's id in Masarak. |
awb | string | yes | The AWB number. |
status | string | yes | The status (CREATED on creation). |
external_reference | string | no | Your reference as sent. |
tracking_url | string | yes | The recipient's tracking link. |
label_url | string | yes | The label's path: /v1/api/shipments/:id/label.pdf (needs labels:read). |
estimate | object | yes | The fees as priced on the merchant's list: delivery_fee_piastres, fee_breakdown, return_fee_piastres. |
delivery_deadline | string | no | The promised deadline (ISO). |
Errors specific to this route (besides the common ones below)
| Status | Code | Description |
|---|---|---|
| 400 | IDEMPOTENCY_KEY_REQUIRED | POST /api/shipments without an Idempotency-Key header. |
| 422 | IDEMPOTENCY_KEY_CONFLICT | The same Idempotency-Key with a different request body. |
| 409 | IDEMPOTENCY_KEY_PROCESSING | A request with the same key is in flight; retry shortly and you read the stored answer. |
| 409 | SHIPMENT_HELD | Nothing was created: details.hold_reason is PAYMENT_POLICY (a prepaid order not paid yet) or CARRIER_DISABLED (the company switched integration orders off). |
| 409 | EXTERNAL_REFERENCE_EXISTS | A shipment with this external_reference exists for this merchant; details carries shipment_id and awb. |
| 400 | INVALID_BRANCH | origin_branch_id is not an active branch of the company. |
| 409 | NO_ORIGIN_BRANCH | The company has no active branch to start the shipment from. |
| 400 | SAME_DAY_CUTOFF_PASSED | SAME_DAY after the branch cutoff. |
| 422 | WEIGHT_OVER_LIMIT | The weight is over the price list's limit. |
| 400 | LOCATION_OUTSIDE_EGYPT | The delivery pin is outside Egypt. |
GEThttps://api.masarakhub.com/v1/api/shipments
List shipments · Required scope: shipments:read · Rate limit: reads — 300 requests a minute
The key's merchant's shipments, newest first, in pages (cursor). Filter by your reference, the AWB or the status.
Query parameters
| Field | Type | Required | Description |
|---|---|---|---|
external_reference | string | no | Your reference, exact. |
awb | string | no | The AWB, exact. |
status | string | no | A shipment status (CREATED, OUT_FOR_DELIVERY, DELIVERED, …). |
limit | integer | no | Page size (default 50, max 200). |
cursor | uuid | no | The previous page's meta.next_cursor. |
Response · 200
| Field | Type | Always present | Description |
|---|---|---|---|
items | array | yes | id, awb, external_reference, status, consignee_name, consignee_city, cod_amount, delivery_fee_piastres, priority, tracking_url, label_url, created_at, updated_at, delivery_deadline. |
meta | object | yes | next_cursor, has_more, limit, count. |
Errors specific to this route (besides the common ones below)
| Status | Code | Description |
|---|---|---|
| 400 | VALIDATION_FAILED | A field is invalid; details[] lists every field with its Arabic and English messages. |
GEThttps://api.masarakhub.com/v1/api/shipments/:id
Shipment details · Required scope: shipments:read · Rate limit: reads — 300 requests a minute
What the merchant sees in the portal: the recipient, the money and fees (financials), the delivery attempts with their evidence when the company shares it, and the status timeline (the actor as a kind, never a staff name).
No body or query parameters; the shipment id travels in the path.
Response · 200
| Field | Type | Always present | Description |
|---|---|---|---|
id | uuid | yes | The shipment id. |
awb | string | yes | The AWB. |
status | string | yes | The current status. |
external_reference | string | no | Your reference. |
financials | object | yes | Expected and collected COD, the fees and what the merchant receives — computed by the server only. |
attempts | array | yes | The delivery attempts in order. |
status_history | array | yes | from_status, to_status, set_at, set_by_actor, attempt_reason, reason. |
Errors specific to this route (besides the common ones below)
| Status | Code | Description |
|---|---|---|
| 404 | SHIPMENT_NOT_FOUND | No shipment with this id on the key's merchant account. |
| 400 | INVALID_ID | An id in the path is not a UUID. |
GEThttps://api.masarakhub.com/v1/api/shipments/:id/label.pdf
The AWB label (PDF) · Required scope: labels:read · Rate limit: reads — 300 requests a minute
The same label PDF the merchant prints from the portal, byte for byte. A cancelled shipment has no label (409 SHIPMENT_CANCELLED).
No body or query parameters; the shipment id travels in the path.
Response · 200
| Field | Type | Always present | Description |
|---|---|---|---|
application/pdf | string | yes | The body is the PDF; Content-Disposition: inline; filename="awb-<AWB>.pdf". |
Errors specific to this route (besides the common ones below)
| Status | Code | Description |
|---|---|---|
| 404 | SHIPMENT_NOT_FOUND | No shipment with this id on the key's merchant account. |
| 409 | SHIPMENT_CANCELLED | A cancelled shipment has no label. |
| 400 | INVALID_ID | An id in the path is not a UUID. |
GEThttps://api.masarakhub.com/v1/api/shipments/:id/tracking
Track a shipment · Required scope: tracking:read · Rate limit: reads — 300 requests a minute
The status with its Arabic and English label, the merchant's timeline, the ETA window while out for delivery and the recipient's tracking link. Status changes also reach you by webhook, so there is no need to poll often.
No body or query parameters; the shipment id travels in the path.
Response · 200
| Field | Type | Always present | Description |
|---|---|---|---|
shipment_id | uuid | yes | The shipment id. |
awb | string | yes | The AWB. |
status | object | yes | code, label_en, label_ar. |
status_history | array | yes | Every status change with its time and actor kind. |
eta_window | object | no | estimate, from, to (ISO) while out for delivery, else null. |
tracking_url | string | yes | The recipient's tracking link. |
Errors specific to this route (besides the common ones below)
| Status | Code | Description |
|---|---|---|
| 404 | SHIPMENT_NOT_FOUND | No shipment with this id on the key's merchant account. |
| 400 | INVALID_ID | An id in the path is not a UUID. |
POSThttps://api.masarakhub.com/v1/api/quotes
Quote a parcel · Required scope: quotes · Rate limit: writes — 60 requests a minute
The fees and promised deadlines for each service level on the merchant's price list, valid for one hour. A service level the list cannot price is left out; SAME_DAY disappears after the cutoff.
Request body
| Field | Type | Required | Description |
|---|---|---|---|
destination_city | string | yes | The destination city. (2 to 80 characters) |
destination_governorate_code | string | no | The governorate code. |
destination_city_code | string | no | The city code. |
destination_address | string | yes | The delivery address. (3 to 500 characters) |
weight_grams | integer | yes | Weight in grams. (1 or more) |
cod_amount | integer | yes | Cash to collect in piastres (0 for prepaid). |
Response · 200
| Field | Type | Always present | Description |
|---|---|---|---|
quote_id | uuid | yes | The quote id. |
expires_at | string | yes | When it expires (ISO). |
options | array | yes | priority, fee_piastres, promised_by per available service level. |
Errors specific to this route (besides the common ones below)
| Status | Code | Description |
|---|---|---|
| 400 | VALIDATION_FAILED | A field is invalid; details[] lists every field with its Arabic and English messages. |
Error codes
Every code you can meet, with its HTTP status. The authentication, scope, account-state and rate-limit codes can come from any route; each route above lists the ones specific to it.
| Status | Code | Description |
|---|---|---|
| 401 | API_KEY_INVALID | The X-Api-Key header is missing or the key is unknown. |
| 401 | API_KEY_REVOKED | The key was revoked; issue a new one from the merchant portal. |
| 403 | API_KEY_SCOPE | The key lacks the scope this route needs; details.required names it. |
| 403 | CLIENT_INACTIVE | The merchant account behind the key is not active. |
| 403 | ORG_READONLY | The shipping company is suspended; reads only. |
| 402 | SUBSCRIPTION_REQUIRED | The shipping company's subscription lapsed; writes are blocked until it pays. |
| 400 | VALIDATION_FAILED | A field is invalid; details[] lists every field with its Arabic and English messages. |
| 400 | LOCATION_OUTSIDE_EGYPT | The delivery pin is outside Egypt. |
| 400 | INVALID_ID | An id in the path is not a UUID. |
| 400 | IDEMPOTENCY_KEY_REQUIRED | POST /api/shipments without an Idempotency-Key header. |
| 400 | INVALID_IDEMPOTENCY_KEY | Idempotency-Key longer than 128 characters. |
| 422 | IDEMPOTENCY_KEY_CONFLICT | The same Idempotency-Key with a different request body. |
| 409 | IDEMPOTENCY_KEY_PROCESSING | A request with the same key is in flight; retry shortly and you read the stored answer. |
| 409 | SHIPMENT_HELD | Nothing was created: details.hold_reason is PAYMENT_POLICY (a prepaid order not paid yet) or CARRIER_DISABLED (the company switched integration orders off). |
| 409 | EXTERNAL_REFERENCE_EXISTS | A shipment with this external_reference exists for this merchant; details carries shipment_id and awb. |
| 400 | INVALID_BRANCH | origin_branch_id is not an active branch of the company. |
| 409 | NO_ORIGIN_BRANCH | The company has no active branch to start the shipment from. |
| 400 | SAME_DAY_CUTOFF_PASSED | SAME_DAY after the branch cutoff. |
| 422 | WEIGHT_OVER_LIMIT | The weight is over the price list's limit. |
| 404 | SHIPMENT_NOT_FOUND | No shipment with this id on the key's merchant account. |
| 409 | SHIPMENT_CANCELLED | A cancelled shipment has no label. |
| 429 | RATE_LIMITED | You passed the key's limit; Retry-After says in how many seconds. |
Idempotency
Header: Idempotency-Key
A key of your choosing (up to 128 characters) on every shipment create. The same key and body within 24 hours return the first answer (the same AWB). The same key with a different body: 422. Keys are isolated per merchant.
Webhooks
Status changes reach your server without polling. Create a subscription in the merchant portal (Webhooks for developers) with a public HTTPS URL and the events you want; the signing secret is shown once. Every delivery is a POST with a JSON body, and besides the signature each message carries these identifying headers (the sequence is an increasing number for ordering and de-duplication):
X-Masarak-Event-Id · X-Masarak-Event-Type · X-Masarak-Event-Sequence
Verifying the signature
Every message carries a signature header. Compute the algorithm below with the subscription secret over the signed payload, compare the result to v1 in constant time, and reject a timestamp t further from your clock than the tolerance. Use the raw body as received, not a re-serialised one.
Header: X-Masarak-Signature
Header format: t=<unix seconds>,v1=<hex HMAC-SHA256>
Algorithm: HMAC-SHA256 · Signed payload: <t>.<raw request body> · Timestamp tolerance: 300 seconds
Events
| Event | Meaning |
|---|---|
shipment.created | A shipment was created. |
shipment.status_changed | A shipment's status changed (data: shipment_id, awb, from_status, to_status). |
shipment.assigned_to_agent | The shipment was handed to a delivery partner. |
shipment.damaged | The shipment was recorded as damaged. |
shipment.returned | The shipment was returned to the merchant. |
cod.collected | Cash on delivery was collected. |
cod.cashed_in | The courier handed the cash in (it became available to the merchant). |
order.created | An order (the shipments envelope) was created. |
pickup.requested | A pickup was requested. |
pickup.assigned | A courier was assigned to the pickup. |
pickup.completed | The pickup was completed. |
pickup.cancelled | The pickup was cancelled. |
pickup.handoff_received | The branch received parcels the merchant handed over. |
pickup.info_responded | The merchant answered an information request on a pickup. |
Payload example
Body fields: event_id, sequence, event_type, occurred_at, aggregate, client_id, data
{
"event_id": "evt_01J9X3V5K8Q2R7T4W6Y8Z0A1B2",
"sequence": 48213,
"event_type": "shipment.status_changed",
"occurred_at": "2026-11-14T09:41:07.000Z",
"aggregate": { "type": "shipment", "id": "0d2f6c0e-7a51-4c3a-9b1e-5f6a7b8c9d0e" },
"client_id": "7c1a2b3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d",
"data": {
"shipment_id": "0d2f6c0e-7a51-4c3a-9b1e-5f6a7b8c9d0e",
"awb": "MK-261114-000123",
"external_reference": "SHOP-10042",
"from_status": "OUT_FOR_DELIVERY",
"to_status": "DELIVERED",
"set_at": "2026-11-14T09:41:07.000Z"
}
}Verification sample (Node.js)
import { createHmac, timingSafeEqual } from 'node:crypto';
// rawBody: the request body as received, before any JSON parsing.
export function verifyMasarakSignature(rawBody, signatureHeader, signingSecret, toleranceSeconds = 300) {
const parts = Object.fromEntries(signatureHeader.split(',').map((kv) => kv.split('=')));
const t = Number(parts.t);
if (!Number.isFinite(t) || Math.abs(Date.now() / 1000 - t) > toleranceSeconds) return false;
const expected = createHmac('sha256', signingSecret).update(`${t}.${rawBody}`).digest('hex');
const given = Buffer.from(parts.v1 ?? '', 'hex');
const wanted = Buffer.from(expected, 'hex');
return given.length === wanted.length && timingSafeEqual(given, wanted);
}
// Express: app.post('/webhooks/masarak', express.raw({ type: 'application/json' }), (req, res) => {
// if (!verifyMasarakSignature(req.body.toString('utf8'), req.header('X-Masarak-Signature') ?? '', process.env.MASARAK_WEBHOOK_SECRET)) {
// return res.status(401).end();
// }
// const event = JSON.parse(req.body.toString('utf8'));
// // handle event.event_type … then answer 2xx quickly.
// res.status(204).end();
// });Answer 2xx within 10 seconds. Otherwise the delivery is retried on a widening ladder; after repeated failure the subscription is paused and the merchant portal shows it.
Rate limits
Per key, per route, per minute: 60 on writes (create shipment, quote) and 300 on reads. Over it: 429 RATE_LIMITED with a Retry-After header in seconds.
Sandbox
There is no separate sandbox host: ask the shipping company for a test merchant account and issue it a key; test shipments are cancelled from the portal.
Versioning
Everything is under /v1. Changes are additive only: new fields may appear; no field or route is removed or changes meaning without a new version.