Using the provided Code Example; While the provided example is written in Python, the same steps apply across all programming languages!
Summary of Authentication Steps:
- Initialize the connection by sending a /public/auth API request with a correctly formatted Client Signature.
- Set a Heartbeat, using /public/set_heartbeat, and respond appropriately to ensure your connection does not become “stale” and disconnected by the Deribit API.
- Use the “refresh_token” provided during each authentication to re-authenticate your application before the time value specified in the “expires_in” authentication response key.
Websocket Connection URLs:
1. Ensure that you have installed the Python package Websockets using pip, Conda, or your package manager of choice.
Useful Command: The ‘pip list’ command will return all of your presently installed packages.
Running something similar to ‘pip install websocket_client’ will install the package into your python environment.
2. Open your IDE (for example, VsCode or Pycharm) and open the Code Example, available on Github, locally.
The provided example code is designed to be a soft introduction to using the Deribit API and to allow anyone to get up and running very quickly.
It is not an example of how things must be or the only solution. It is designed for beginners and experts alike to help colour the API Documentation.
The provided code is an example only and Deribit is not responsible for any actions as a result of running, using, or deploying the provided example.
Keep in mind this Github will be updated quite quickly and the contents are likely to change.
3. In the just-opened Code Example project, navigate to the “User-Credentials” folder and open the “UserCredentials.py” file.
4. Replace the existing values mapped to the “Client_Id” and “Client_Secret” variables.
Use the “Client_Id” and “Client_Secret” values of your API Key we created at the end of the previous section.
Remember to save the file!
5. Navigate to and open the “JSON-RPC_Websockets-Example.py” file in the “JSON-RPC_Websockets” directory (folder).
6. On Line 14, update the existing directory path with the path to your “User-Credentials” folder.
This is the folder path available using Windows Explorer for example.
Remember to save the file!
7. Run the file “JSON-RPC_Websockets-Example.py”.
This will produce a similar output to the following image.
Running this example code will:
This example also produces an output of the present positions in your portfolio using the /private/get_positions endpoint.
We encourage you to pull apart the code and try things line by line.
Key Code to note:
- The “scope” variable, on Line 234, allows you to choose between “read” and “read-write”. This is a choice between allowing your program to only “read” from your account or to be able to “write”, such as trading and withdrawing capital. This does not supersede the privileges granted to each API Key during creation, but limits the application’s authenticated scope.
- The “exchange_version” variable, on Line 236, allows you to choose between using the testnet and live exchange.
- In the class “UserWebsocketEngine” you can update the “self.seconds_delay” variable if you want to increase the speed at which your application requests from the API. Be careful though, we do have rate limits which specifically apply to requests that affect our matching engine.
The Deribit API Console is a fantastic resource. It allows you to test queries in a web interface and observe the raw requests and real responses returned.