How To Get Started With Biostar 2 Api: Compiled by George Cartlidge
How To Get Started With Biostar 2 Api: Compiled by George Cartlidge
How To Get Started With Biostar 2 Api: Compiled by George Cartlidge
BioStar 2 API
Compiled by
George Cartlidge
What is the API or SDK, and what is the difference?
API stands for Application Programming Interface, where SDK stands
for Software Development Kit.
An SDK is a collection of tools that you can use to develop your own
applications using a particular framework/platform. Regarding the
BioStar 2 SDK, this will be the BS_SDK_V2.dll file. This .DLL contains
many APIs that can be used with C++ & C# programming languages.
There is also the new G-SDK which functions along the same principal
but is based upon gRPC which can support more languages that the
existing SDK. Using the SDK, you cut BioStar 2 out completely and
directly interact with the device.
Comparing both, the API will be used where you are happy for
BioStar 2 to handle most of the work, BioStar 2 would be storing the
fingerprint and user data; The API lets you use BioStar 2 from inside
another application, so it does not look like BioStar 2. The SDK will be
used when you want to connect directly to the device from your own
application; Your application would have to handle the connection
and the storage of data. The SDK will also allow you to use specific
API’s that are not available in the BioStar 2 API.
2
API – Swagger!
Previously, the BioStar 2 API had to be installed separately but
starting with BioStar 2.7.10, there is a new API that includes more
calls and more features that is packaged together with the install of
BioStar 2, so there is no need to install the Local API Server. The local
API server is still supported but will no longer be updated. In this
document, I will be using the new BioStar 2 API.
You can see above (And from your screen hopefully) some examples
of API calls, you will be able to test some of these by browsing to the
IP of your machine followed by the call
3
(EG. https://*IP_OF_MACHINE*/api/access_levels/)
Browsing to this page however should bring up ‘login required’ on
the response.
To login to the API, you require a bs-session-id that will then be
passed along with the header in all future calls. We can try this
ourselves using the Swagger UI and ‘/api/login’.
NOTE: The username and password will be the admin account given
during initial setup (Username should always be ‘admin’)
If you click the ‘Try it out’ button next to parameter, you will be able
to enter your own Username and Password (This should be the one
used for logging into BioStar 2) and click execute! This just uses the
CURL command to poll the API web link. The response body will
provide information about the account that you have used to login
4
Where the response header will provide you with the bs-session-id
Once this has been inputted, you will be able to use the rest of the
API calls, as the swagger will automatically pass this bs-session-id
along when posting to the APIs.
5
API - Using cURL
Our API swagger is a good view of how you would go about
interacting with the BioStar 2 server, you could even do something
very simple like interacting with BioStar 2 with CMD (I added a little
bit of PowerShell to hide the password) and cURL commands!
When using the API swagger, clicking try it out next to any calls (Like
above) will let you edit the parameters.
6
You can then click ‘Execute’, this will send the command to the server
using cURL, you can see and take a note of the curl command below
the execute button.
Now we have a curl command, you can input this through CMD to
interact with the server, which should give the same response as the
swagger.
In the next steps, you’ll see how we can do this using only CMD.
7
API – Login!
The below batch script is an example of the login API using the curl
post generated by the swagger, where you can enter the IP of the
server (It is worth noting, to use the API with https, you will need the
SSL certificate installed on the machine), enter a username/password
and it logs you in with a curl output response that contains a valid bs-
session-id, the end of the script then outputs that line to a separate
.txt file that will be used in the next script!
@echo off
:: Prompts user for IP of the server, Sets variable then exports to a .txt file
set /P IP=
echo.
set /P UserID=
echo.
::Prompts user for password input but hides the characters using PowerShell, then sets as variable
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; ^
::Uses cURL to sent the command to server, using variables inputted above, exports to curloutput.txt
::Searches curloutput.txt for the BS-Session-ID Line and exports it singularly to a .txt file.
8
API – Add User!
The below batch script is again very simplistic but allows you to input
a user into BioStar 2 using the curl command generated by the
swagger. It first uses the BSSession.txt generated by the Login script
previously used and sets this as a variable, you then just need to
enter the ID, name and email of the new user.
@echo off
:: Uses files previously generated using API Login.cmd to set IP and BS-Session-ID
set /P ID=
echo.
set /P Name=
echo.
set /P Email=
echo.
9
This is setting the basics of the user, as within this, I have set ‘User
group ID’ to always be ‘1’, as well the start and expiry to the default
BioStar 2 values (2001 – 2030).
10