If you want to use CodeScene's REST API, you need to simply create a dedicated user with a RestApi role or generate a personal access token. You will then use them to authenticate.
Generate the Personal Access Token
To generate the Personal Access Token and use it for the REST API, please follow the steps below:
- Navigate to the Configuration -> Authentication -> Internal User Management -> Personal Access Tokens.
- Add the Description and click on the Generate token.
- Make sure to copy the token.
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 the 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:
- With username & password:
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.
- With personal access token:
-
curl -X GET --header "Accept: application/json" --header "Authorization: Bearer <token>" "http://localhost:3003/api/v2/projects"
-
-
- Replace '<token>' with the personal access token you just created.
-
The example output:
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:
