Developer documentation

    Add the UPCS map to your website or app. Visitors click a spot on the grid and you receive its postcode as JSON.

    Overview

    The embedded map shows the UPCS grid and nothing else: no dialogs, no UPCS branding to style around. Your page never sees your API key, and the postcode rules stay on UPCS servers. The map runs inside a UPCS-hosted frame that your page cannot read.

    1. Your server exchanges its secret API key for a short-lived session token.
    2. Your page mounts the map with that token.
    3. When the visitor clicks a cell, your onSelect callback receives the postcode JSON.

    Get an API key

    1. Create a partner account with your business details. Your first API key (starting with upcs_) is created for you.
    2. Open the partner panel to view, copy, disable or delete keys, or generate more with Generate API key.
    3. Store the key in your server configuration, for example an environment variable called UPCS_API_KEY.
    • One key, every integration. The same key works in the WordPress, PrestaShop and OpenCart plugins and in your own custom software. Create separate keys per site if you want to disable them independently.
    • Each key has an hourly request limit (1,000 per hour by default).
    • Send the key in the x-api-key header of every server-to-server request. Base URL: https://api.upcs.io/api

    Allowed origins

    For custom software, register every website that will show the map under Allowed origins in the partner panel. An origin is the exact address of the page, such as https://shop.example.com (https only; http://localhost:5173 works for development).

    • You can add several origins, change them, and remove them at any time.
    • A map session is only created when the origin you send matches a registered one, and the map and its results are only delivered to that origin.
    • Removing an origin immediately cuts off sessions already issued for it.

    Embed the map

    Step 1 — create a session on your server

    Call this when the page is rendered. origin is the exact address of the page that will show the map, and it must be one of your registered allowed origins. Only that origin can display the map and receive results. The token is valid for 15 minutes by default; pass ttl (60–3600 seconds) to change it.

    curl -X POST https://api.upcs.io/api/v1/embed/sessions \
      -H "x-api-key: upcs_your_key_here" \
      -H "Content-Type: application/json" \
      -d '{"origin": "https://shop.example.com"}'

    The response contains session_token, expires_at (Unix time) and embed_url.

    Step 2 — mount the map on your page

    <div id="upcs-map"></div>
    <script src="https://api.upcs.io/api/embed/upcs-embed.js"></script>
    <script>
      UPCS.mount('#upcs-map', {
        sessionToken: '<token from your server>',
        height: '480px',
        onSelect: function (postcode) {
          // postcode.upcs_code, postcode.latitude, postcode.longitude ...
          document.getElementById('upcs-code').value = postcode.upcs_code;
        },
        onError: function (err) {
          console.warn(err.code, err.message);
        }
      });
    </script>
    OptionRequiredDescription
    sessionTokenRequiredToken returned by your server from the create-session call.
    heightOptionalCSS height of the map. Default 480px.
    centerOptional{ lat, lng } the map opens on.
    zoomOptionalOpening zoom. The grid appears from zoom 18.
    styleOptionalstreets (default), satellite, traffic or biking.
    onSelectOptionalCalled with the postcode JSON when a cell is clicked.
    onErrorOptionalCalled with { status, code, message } when a request fails.
    onReadyOptionalCalled once the map has loaded.

    UPCS.mount returns { iframe, destroy() }. Call destroy() to remove the map. When a session expires, onError reports session_expired; request a new token and mount again.

    Click response

    Your onSelect callback receives this object each time a cell is clicked:

    {
      "success": true,
      "upcs_code": "PK-1A2B-3C4D",
      "latitude": 24.860712,
      "longitude": 67.001134,
      "address": "Clifton, Karachi, Pakistan",
      "country_code": "PK",
      "cell": {
        "bounds": { "north": 24.8607, "south": 24.8606, "east": 67.0012, "west": 67.0011 },
        "center": { "lat": 24.860712, "lng": 67.001134 }
      },
      "saved": false,
      "share_url": "https://api.upcs.io/PK-1A2B-3C4D"
    }
    FieldMeaning
    upcs_codeThe postcode of the clicked 3 m × 3 m cell, e.g. PK-1A2B-3C4D.
    latitude / longitudeCentre of the clicked cell.
    addressReverse-geocoded address. Falls back to the coordinates if none is found.
    country_codeTwo-letter country prefix used in the code.
    cell.bounds / cell.centerThe cell as a bounding box and its centre, if you want to draw it yourself.
    savedtrue if the postcode was stored (server-to-server calls with save: true).
    share_urlPublic link that resolves the postcode.

    Server-to-server

    If you already have coordinates and don't need the map, ask for the postcode directly. The response has the same shape as above. Add "save": true to store the postcode so it can be found later with the lookup endpoint; by default nothing is stored.

    curl -X POST https://api.upcs.io/api/v1/embed/resolve \
      -H "x-api-key: upcs_your_key_here" \
      -H "Content-Type: application/json" \
      -d '{"lat": 24.8607, "lng": 67.0011, "save": true}'

    Other endpoints

    All requests use https://api.upcs.io/api and the x-api-key header.

    Look up a postcode

    GET /v1/upcs/lookup/{code} returns upcs_code, a location object (latitude, longitude, address, label, country_code, country_flag), map links and share_url. Unknown codes return 404.

    curl https://api.upcs.io/api/v1/upcs/lookup/PK-1A2B-3C4D \
      -H "x-api-key: upcs_your_key_here"

    Generate and store a postcode

    POST /v1/upcs/generate with latitude, longitude and an optional address or label. It returns upcs_code, is_existing and share_url.

    curl -X POST https://api.upcs.io/api/v1/upcs/generate \
      -H "x-api-key: upcs_your_key_here" \
      -H "Content-Type: application/json" \
      -d '{"latitude": 24.8607, "longitude": 67.0011, "address": "Clifton, Karachi"}'

    Supported countries

    Postcodes can be generated for locations in these countries. Points elsewhere return unsupported_location.

    Supported countries

    Errors & limits

    Errors return JSON with success: false, a message and, where relevant, a machine-readable code.

    HTTPcodeMeaning
    401session_expiredSession token missing, tampered with or expired. Create a new session.
    401key_inactiveYour API key was disabled, expired or revoked.
    403partner_requiredThe key is not linked to a partner account. Use a key from your partner panel.
    403origin_not_allowedThe origin is not registered in your partner panel (or was removed).
    401—Missing or invalid x-api-key on a server-to-server call.
    403—The request came from an IP address not allowed for this key.
    422unsupported_locationThe point is outside the countries UPCS currently covers.
    422—Validation error, for example an invalid origin, lat or lng.
    429rate_limitedToo many requests for the session, or the key reached its hourly limit.

    Security

    • Create sessions only from your server. The API key never goes to the browser.
    • A session works only for an origin registered in your partner panel and expires on its own.
    • Disabling a key, or removing an origin, immediately stops the sessions that depend on it.
    • The map is embedded in an iframe served by UPCS, so your page cannot read or modify its contents. You only receive the JSON it sends you.

    Using WordPress, WooCommerce, PrestaShop or OpenCart? The ready-made plugins use the same API key.