Using CodeScene's REST API - On prem

If you want to use CodeScene's REST API, you need to simply create a dedicated user with a RestApi role. You will then use the username and password to authenticate. 

Create a user with a RestApi role

To create a dedicated user with a RestApi role, please follow the steps below:
  • Log in as an administrator.
  • Navigate to the Configuration -> Authentication.
  • Scroll down to the Add New User section, specify the username and password, and click on the Add User button.
  • Under the All Users section, find a user you just created and change its role to 'RestApi'.


REST API Functions

You can browse the REST API functions here: CodeScene RestAPI Functions.
This link is available from your running CodeScene instance top menu -> Documentation -> You then search for REST API -> Open it -> Scroll down to The REST API documentation URL and click on the CodeScene RestAPI Functions. Before trying any functionality, use the Authorize button then fill in your username and password to authenticate.

You can find more details about CodeScene's REST API and its usage: REST API in our documentation.


Example of running the REST API call

A REST API call to get a list of all projects:
You can run the curl command in your terminal or command prompt on the machine where your CodeScene instance is hosted. In our example the API call targets http://localhost:3003, and this should be executed on the machine where CodeScene is running:
  • Open a terminal or command prompt on the machine where CodeScene is hosted.
  • Run the curl command:
curl -X GET --header "Accept: application/json" -u username:password "http://localhost:3003/api/v2/projects"
  • Replace 'username:password' with the correct username and password for a user with the REST API role in CodeScene.

The example output:

image-Sep-04-2024-01-45-53-9611-PM

Example of using the scripts

The usage of scripts is based on the following strategy:
  • Invoke the REST API using curl.
  • Parse the reply with the jq tool.
  • Pipe the response to other shell functions when needed.
You can start by defining a helper function for calling the REST API:
call_api(){ curl -sS -u api:secret http://localhost:3003/api/v2$1 ${@:2}; }

export -f call_api
Using the call_api utility, you can query the REST API to get the desired information.
More information here: Examples: Use cases and scripts.
 
An example of how a script for getting the file-level analysis information could look like:
image-Sep-04-2024-02-24-35-3425-PM