Appearance
Getting the version from the API
You can use the /v1/version
endpoint to check the version of the API.
Getting the version - Curl
shell
curl 'https://api.unopdf.com/v1/version' \
-X 'GET' \
-H 'Content-Type: application/json'
Getting the version - Nodejs
ts
const method = "GET";
const headers = { "Content-Type": "application/json" };
const url = `${api_url}/v1/version`;
const response = await fetch(url, {
method,
headers,
});
Code
ts
import dotenv from "dotenv";
dotenv.config();
import { getError } from "./errorhandling";
interface VersionResponse {
apiVersion: string;
minimumNpmPackageVersion: string;
}
function isValidVersionResponse(json: unknown): json is VersionResponse {
if (
json &&
typeof json == "object" &&
"apiVersion" in json &&
"minimumNpmPackageVersion" in json
) {
return true;
}
return false;
}
async function getVersion(api_url: string): Promise<VersionResponse> {
// #region get-version-endpoint
const method = "GET";
const headers = { "Content-Type": "application/json" };
const url = `${api_url}/v1/version`;
const response = await fetch(url, {
method,
headers,
});
// #endregion get-version-endpoint
if (response.status !== 200) {
const error = await getError(response);
console.error(error);
throw new Error(`Error "${error}" calling UnoPdf API "${method} ${url}".`);
}
const json = (await response.json()) as object;
if (!isValidVersionResponse(json)) {
throw new Error(
`Unexpected response "${JSON.stringify(
json
)}" from UnoPdf API "${method} ${url}".`
);
}
return json;
}
const api_url = process.env.UNOPDF_API_URL;
if (!api_url) {
throw new Error("No UNOPDF_API_URL setting specified");
}
console.log(`Checking version of ${api_url}`);
getVersion(api_url).then((version) => {
console.log(`Api Version: ${version.apiVersion}`);
console.log(
`Minimum Npm Package Version: ${version.minimumNpmPackageVersion}`
);
});
Running the example
- Download the rest - Getting the version from the API project
- Unzip the file to a directory
rest-get-version
.shellunzip rest-get-version.zip -d rest-get-version
- Open a terminal and go to that directoryshell
cd rest-get-version
- Install dependenciesshell
npm install
shellyarn
- Start the projectshell
npm run start
shellyarn start