@patroltech/sdk-api
v2.0.4
Published
Official TypeScript SDK for the PatrolTech public REST API, generated from its OpenAPI contract
Maintainers
Readme
PatrolTech TypeScript SDK
Official TypeScript client for the PatrolTech public REST API: integrate guard tour workflows (routes, checkpoints, rounds, scans, incidents) into your own applications.
- API reference: every endpoint, parameter and response of the public API, browsable online.
- Generated from the public contract
openapi/openapi.public.json, the same document the API serves athttps://api.patroltech.online/openapi.public.json. - Typed requests and responses, ESM and CommonJS builds, no runtime dependencies (it uses the platform
fetch). - Works in Node.js 22+ (which loads the CommonJS build, for
requireandimportalike) and in browsers through a bundler (ESM build). Types need TypeScript 5.0+.
Create a free PatrolTech account · Use PatrolTech from an AI assistant
Install
npm install @patroltech/sdk-apiAuthentication (OAuth 2.1)
PatrolTech uses the OAuth authorization code flow with PKCE (S256) and public clients (no client secret). The discovery document is at https://api.patroltech.online/.well-known/oauth-authorization-server.
- Register your client once with dynamic client registration (
registerOAuthClient, RFC 7591), or use the URL of a Client ID Metadata Document asclient_id. - Send the user to the consent screen built with
getStartOAuthAuthorizationUrl. Scopes:read,write. PatrolTech redirects back to yourredirect_uriwithcodeandstate; check thatstateis the one you sent. - Exchange the code for an access and refresh token with
requestOAuthToken(grant_type: 'authorization_code'). Rotate them later withgrant_type: 'refresh_token'; revoke them withrevokeOAuthToken. - Open an API session:
createOAuthAgentSession, called with the OAuth access token as Bearer, returns a short-livedtoken(andexpires_at) for the/api/*endpoints. Open a new one before it expires.
The whole flow, step by step and type-checked in CI, is in examples/oauth.ts. What a session can do is decided by the API: the user's role in the organization and the scopes they granted. The SDK grants no access by itself.
Keep tokens out of logs, URLs and source control. Store the refresh token server-side.
Quick start
import { configureApiSdk, listRoutes } from '@patroltech/sdk-api';
let session = await openApiSession(oauthAccessToken); // step 4
configureApiSdk({
baseUrl: 'https://api.patroltech.online',
getAccessToken: () => session.token,
// Optional. On a 401 the SDK calls this once and retries with what getAccessToken()
// returns now: update the session it reads and return its token, or null to give up.
onUnauthorized: async () => {
session = await openApiSession(oauthAccessToken);
return session.token;
},
});
const response = await listRoutes({ organizationId, page: 1, limit: 20 });
if (response.status === 200) {
console.log(response.data.results.map((route) => route.name));
}If several requests can fail at once, share one refresh between them (keep the pending promise) instead of opening a session per 401.
Responses and errors
- Every function returns
{ data, status, headers }. For/api/*the{ success, message, data }envelope is unwrapped, sodatais the payload described by the contract./oauth/*responses are plain JSON, as in the OAuth RFCs. - A non-2xx response throws an
ApiErrorwithstatus,messageand the response body indata(its type is exported asApiErrorBody). Checkingresponse.status === 200only narrows the TypeScript type. - A
423for a suspended organization also callsonSuspended(body)if you configured it. - Path parameters are percent-encoded (
x/detailsstays one segment), and an id that is exactly.or..is refused with aTypeErrorbefore any request, so untrusted input cannot reach another endpoint with your token. - Redirects are not followed: a 3xx rejects with a
TypeError, so a redirect cannot carry custom headers or a request body to another host. Passredirect: 'follow'on the call if you expect one. A path that does not start with a single/is also refused with aTypeError.
One configuration per request
configureApiSdk sets a global configuration. A server acting for several users can bind one per call instead:
import { createApiClient, listScans } from '@patroltech/sdk-api';
const client = createApiClient({ baseUrl, getAccessToken: () => tokenOfThisUser });
await listScans({ organizationId }, client.init());init() accepts extra RequestInit options, such as signal for an AbortController. Pass headers as a plain object ({ 'X-Request-Id': id }), not a Headers instance.
Postman, Bruno or Insomnia
Import the contract URL https://api.patroltech.online/openapi.public.json directly in Postman, Bruno or Insomnia to try the API without writing code.
Webhooks
Webhooks are in development. Tell us which events you need at [email protected].
Versioning
- The contract has its own semantic version (
info.version). A breaking change in the API requires a new major version of the contract. - The SDK version follows its public TypeScript surface (exports, function signatures and exported types), not only the contract: a contract change that is compatible over HTTP can still change a signature (a new query parameter goes before
options), and that is a new major version of the SDK.scripts/api-surface.mjscompares the built package with the published one; thePublic API surfacecheck fails a pull request whose version goes up less than its change needs. A new major version of the contract also means a new major version of the SDK. - Each contract change reaches this repository automatically as a pull request with that version bump and the list of signatures that change. When its CI passes, a minor or patch release merges itself and is published to npm with provenance; a major one waits for a maintainer's review.
Contributing
The client is generated with orval from the contract: do not edit src/generated/ (it is not committed) or openapi/ (it comes from the PatrolTech API). Bug reports and improvements to the hand-written parts (src/mutator.ts, examples, docs) are welcome.
npm ci
npm run build # generate + compile ESM and CJS
npm test
npm run test:scripts # tests of scripts/ (node --test)
npm run examples:check
npm run format:check
npm run api-surface -- report # what changes against the latest version on npmSecurity issues: see SECURITY.md. Do not include credentials, tokens or customer data in public issues.
About PatrolTech
PatrolTech is guard tour and security patrol software developed by Ingenieros Web SL in Madrid, Spain. It combines QR/NFC checkpoint recording, offline patrol records, incident reports and a management dashboard.
Integration enquiries: [email protected].
AI agents and MCP
For Claude, ChatGPT, Cursor or Gemini to operate an existing PatrolTech account, use the remote MCP connector and agent skill. Use this SDK when building your own application or agent integration against the public REST API. Both require the account owner's OAuth authorization; a package installation grants no account access.
En español
SDK oficial en TypeScript de la API pública de PatrolTech, generado desde el contrato OpenAPI público. Instálalo con npm install @patroltech/sdk-api; la autenticación es OAuth con PKCE (ver examples/oauth.ts). Referencia de la API en patroltech.github.io/sdk-api; el contrato openapi.public.json se importa directamente en Postman, Bruno o Insomnia. Los webhooks están en desarrollo: cuéntanos qué eventos necesitas en [email protected].
License
MIT © Ingenieros Web SL
