Bardiya Map — Dart SDK
Official Dart client for the Bardiya Map public v1 API: Places, Geocoding, Directions, Distance Matrix, Address Validation, Tracking, Client Tokens, Subscriptions, Webhooks and Realtime (SSE).
Status: release-qualified, not published. Version
0.1.0passesdart analyze,dart testanddart pub publish --dry-run; publication waits for the verifiedbardiyamap.compublisher on pub.dev. Until then the package is usable as a path or Git dependency.
Install
dependencies:
bardiyamap_sdk:
path: ../sdk/dart
import 'package:bardiyamap_sdk/bardiyamap_sdk.dart';
Requires Dart 3.4 or newer. Pure Dart: it runs on the server, in Flutter apps, and in command-line tools.
Credentials
Two credentials exist and are not interchangeable:
| Surface | Credential | Transport |
|---|---|---|
| Maps (places, geocode, directions, matrix, address validation, style, tiles) | Maps API key | X-API-Key |
| Tracking (sessions, history, state, client tokens, webhooks, subscriptions) | Tracking API key | Authorization: Bearer <key> |
| Device ingest (one session, two write operations) | short-lived ingest credential | Authorization: Bearer <credential> |
final client = BardiyaClient(BardiyaConfig(
credentials: BardiyaCredentials(
mapsApiKey: Platform.environment['BARDIYA_MAPS_API_KEY'],
trackingApiKey: Platform.environment['BARDIYA_TRACKING_API_KEY'],
),
));
Never ship a server API key inside a mobile or browser application. A Flutter app should call Maps/Tracking through a customer-authenticated backend proxy, or use a session-scoped ingest credential for device writes. The ingest credential can only post locations for its one session; it cannot read or administer anything.
Read plane
Tracking GET reads (trackingRead, clientTokensRead, webhooksRead) are sent
to read-api.bardiyamap.com. The transport is read-only: a non-GET call throws
StateError before any request is made. Writes and SSE streams use the write
plane (api.bardiyamap.com). Maps reads stay on the write plane because they
carry the Maps key.
Errors, retries and timeouts
Non-2xx responses become BardiyaApiException with error.code,
error.message, error.requestId and error.retryAfter.
- Retries: GET/HEAD on transient failures. Chargeable POST computations
(
/v1/directions,/v1/distance-matrix,/v1/validate-address) retry only whenretryPostComputationsis enabled and the call supplies anidempotencyKey; even then only an ambiguous transport failure/timeout is replayed. The header is accepted on every metered Maps route, including GET reads; the ledger deduplicates debit and recomputes rather than caching the body. The server-side idempotency ledger is flag-gated off in production, so no exactly-once guarantee is claimed. - Writes are never replayed.
- Retry budget:
maxRetries(default 2), exponential backoff,Retry-Afterhonoured and capped atmaxRetryDelay(default 30 s). - Timeouts:
requestTimeout(default 30 s) maps tocode: timeout. - Transport failures map to
code: transport_error.
Places and routing
final places = await client.places.searchPlaces(
'قهوة',
latitude: 30.0444,
longitude: 31.2357,
limit: 3,
);
final directions = await client.directions.directions(
DirectionsPublicRequest(
origin: PublicCoordinate(latitude: 30.0444, longitude: 31.2357),
destination: PublicCoordinate(latitude: 30.0626, longitude: 31.2497),
),
xStrictRoadNetwork: 'true',
);
final forward = await client.geocoding.forwardGeocode(
'25 شارع التحرير، الدقي',
latitude: 30.0397,
longitude: 31.2035,
limit: 10,
language: 'ar',
);
final reverse = await client.geocoding.reverseGeocode(
30.0397,
31.2035,
nearbyLimit: 5,
language: 'ar',
);
Distance Matrix
The runtime accepts at most 25 origins and 25 destinations, with a maximum of 625 origin × destination elements. This example uses five origins and one destination:
final origins = <PublicCoordinate>[
PublicCoordinate(latitude: 30.0444, longitude: 31.2357),
PublicCoordinate(latitude: 30.0500, longitude: 31.2400),
PublicCoordinate(latitude: 30.0550, longitude: 31.2450),
PublicCoordinate(latitude: 30.0600, longitude: 31.2500),
PublicCoordinate(latitude: 30.0650, longitude: 31.2550),
];
final matrix = await client.distanceMatrix.distanceMatrix(
DistanceMatrixPublicRequest(
origins: origins,
destinations: [
PublicCoordinate(latitude: 30.0626, longitude: 31.2497),
],
profile: 'driving',
),
);
Device ingest
First create or obtain a session, and mint the session-scoped ingest credential through the server-side tracking API:
final session = await client.tracking.createSession(
PublicSessionCreate(
subjectType: 'shipment',
externalReference: 'SHIPMENT-12345',
),
idempotencyKey: 'session-SHIPMENT-12345',
);
final sessionId = session!.id;
final state = await client.trackingRead.getState(sessionId);
final history = await client.trackingRead.getHistory(
sessionId,
limit: 200,
);
final credential = await client.clientTokens.issueIngestToken(sessionId);
final ingestCredential = credential!.ingestCredential;
final ingest = client.ingestFor(
sessionId: sessionId,
credential: ingestCredential,
);
await ingest.sendLocation(TrackingIngestEventCreate(
clientEventId: 'evt-1',
clientSeq: 1,
recordedAt: DateTime.now().toUtc(),
latitude: 30.0444,
longitude: 31.2357,
));
Ingest writes are configured with zero retries. close() on a client created
with an injected http.Client does not close that client.
Error Handling
Generated HTTP calls and hand-written wrapper calls normalize failures into
BardiyaApiException.
try {
await client.distanceMatrix.distanceMatrix(
DistanceMatrixPublicRequest(
origins: origins,
destinations: [PublicCoordinate(
latitude: 30.0626,
longitude: 31.2497,
)],
),
);
} on BardiyaApiException catch (error) {
final status = error.error.statusCode;
final code = error.error.code;
final requestId = error.error.requestId;
if (status == 429) {
// Honor error.error.retryAfter when present.
}
}
Realtime tracking
final stream = client.streamSession(session.id);
await for (final event in stream) {
switch (event.event) {
case 'snapshot':
final snapshot = event.decoded;
break;
case 'location':
final location = event.decoded;
break;
case 'closed':
break;
}
}
The stream is a single-subscription SSE connection. The API does not support
resume: on failure, reconnect and re-read the snapshot. Pass credential: to
streamSession to use an ingest credential instead of the tracking key.
A 200 response without text/event-stream, a non-200 response, a transport
failure or a connection timeout all raise BardiyaApiException with a typed
code (protocol_error, the server envelope code, transport_error,
timeout).
The REST client compiles to JavaScript (dart compile js) and has no dart:io
dependency. Incremental SSE delivery in a browser depends on
package:http's browser client and is not qualified by this repository; use
the stream on the Dart VM or Flutter mobile.
Webhooks
final ok = WebhookSignature.verify(
secret: endpointSecret,
timestamp: timestamp, // X-Bardiya-Webhook-Timestamp (Unix seconds)
body: rawBodyBytes,
signature: signatureHeader, // X-Bardiya-Webhook-Signature
);
Pass the full v1=<lowercase hex> header and the exact raw body. The helper
authenticates the timestamp but does not check freshness: reject timestamps
outside your replay window separately. Unsupported versions return false;
set throwOnUnknownVersion: true to receive a FormatException instead.
Regeneration
Generated sources live in lib/src/generated/ and are never edited by hand.
# one-time: openapi-generator-cli-7.27.0.jar from Maven Central
cd sdk/dart
dart pub get
python generate.py --jar /path/to/openapi-generator-cli-7.27.0.jar
Without --apply the command is a drift check: it regenerates into a temporary
directory, applies the pinned post-generation patches, and exits non-zero if any
new, missing or stale .dart file differs (line endings normalized). Add
--apply to write the tree. The JAR SHA-1 is verified before generation.
Validation
dart analyze --fatal-infos
dart test
python generate.py --selftest
The suite covers auth routing, read-plane isolation, ingest, retries, timeouts,
error mapping, SSE framing/failures, the shared contract fixtures, webhook
signatures and 39/39 operation coverage. A small web entry importing the package
compiles with dart compile js (233,799 bytes), proving the generated client no
longer depends on dart:io. No test targets the live production API; all use
mocks, fixtures or local fakes.
License
Apache-2.0. See LICENSE and NOTICE in this directory. Applies to the SDK
only; the Bardiya Map service and data are licensed separately.
Libraries
- bardiyamap_sdk
- Official Bardiya Map SDK for Dart.