Skip to main content

Use HTTP API

1 Overview

Hesai HTTP API provides a standard HTTP interface that allows developers to interact with lidar devices using simple HTTP commands. With this API, you can easily perform operations such as retrieving lidar parameters and configuring features.

Note: HTTP API is only applicable to Hesai lidar models that support HTTP servers, including the Pandar series, QT series and XT series.

2 Application Method

The application method of HTTP API commands is introduced as follows:

  1. Connect the lidar to the host computer and ensure proper communication is established (refer to Configure Ethernet for details);
  2. Send HTTP API commands from the host computer to the lidar. It is recommended to use Python scripts for this purpose. A sample code is provided below:
#!/usr/bin/env python3

# -*- coding: utf-8 -*-

from urllib import request, parse
import requests
import json
import sys
import getopt
import IPy
import time
import os

## Lidar IP address

ipaddr = "192.168.1.201"

## HTTP API command

local_url = 'http://' + ipaddr + '/pandar.cgi?action=get&object=lidar_config'
f = None # Initialize f
try:
f = request.urlopen(local_url, timeout=3.0)
print("Success!")
data = f.read()
print(data)
print('Status:', f.status, f.reason)
json_data = data.decode('utf-8')
py_dict = json.loads(json_data)
py_body = py_dict.get('Body')

except Exception as e:
print(f"Error connecting to {local_url}: {e}")
finally:
if f:
f.close()

In the above sample code, there are two parameters that require special attention:

  • ipaddr: The IP address of the lidar
  • local_url: The string corresponding to the HTTP API command

During actual operation, these two parameters need to be updated based on the lidar's IP configuration and the purpose of the HTTP API.

  1. Retrieve the response from the lidar to confirm whether the API command was successfully sent.

3 Examples

This document provides some common use cases as examples to illustrate the usage of the HTTP API.

Note: The following examples are based on the model of XT32M2X. The HTTP API commands for different lidar models may vary. Please refer to the HTTP API manual of the specific model.

3.1 Retrieve Lidar Parameters (GET)

  1. Set the ipaddr parameter in the sample code according to the current IP address of the lidar;
  2. Modify the local_url in the sample code as follows (this example retrieves basic information about the lidar), and run the Python script on the host computer:

## HTTP API command

local_url = 'http://' + ipaddr + '/pandar.cgi?action=get&object=device_info'
  1. The output will include basic information about the lidar, e.g. SN, MAC address, firmware version, etc. Here is the output example:
Success!
b'{"Head":{"ErrorCode":"0","Message": "Success"},"Body":{"SN": "XT3ACD5F983ACC56","Mac": "EC:9F:00:01:58:3D", "SW Ver: "2.1.7","HW Ver":"1.0.0","FW Ver":"2.0.11","Model":"XT32M2X","Angleoffset":"9129", "Calibsize":"0","FPGA_Ver":"","LidarCalibsize":"384","Up_Fpga_Ver":" 2.1.4","Udp_Seq":"1","1","ProdDate":"2023-06-08", "ProdName":"XT32M2X-B01","LidarDataformat":"1","Trigger_Method":"0","LaserNum":"32","Firing_Method":"",""AccAnglePhase":"0","AccAn
glePeak": "0","AccAngleEnable":"0","0","MotorType":"0", "LargeCodeRange":"1","Codewheel_cal":"1","MotorStartAngle_en":"0", "MotorstartAngle_val":"0","UpFpgaconfigsha":"f80c28c5", "UpFpgacorrectsha":"ef480d97","PN":""XT32M2X"}}'
Status:200 OK

3.2 Configure Lidar Parameters (SET)

  1. Set the ipaddr parameter in the sample code according to the current IP address of the lidar;

  2. Modify the local_url in the sample code as follows (this example configures the lidar to dual return mode), and run the Python script on the host computer:


## HTTP API command

local_url = 'http://' + ipaddr + '/pandar.cgi?action=set&object=lidar_data&key=lidar_mode&value=2'
  1. If the following output is displayed in the terminal, the configuration was successful:
Success!
b'{"Head":{"ErrorCode":"0","Message": "Success"}}'
Status:200 OK
  1. Open a web browser, enter the lidar's IP address and confirm that the return mode configuration is already effective on the lidar's control page:
http_dualreturn