Building a LimeSurvey API Client and Data Warehouse Sync

Building a LimeSurvey API Client and Data Warehouse Sync

Survey data becomes far more useful when teams can retrieve it consistently, inspect its structure, and analyze responses alongside other information. This development effort delivers a Java client and command-line interface for LimeSurvey’s RemoteControl API, then adds a repeatable synchronization path that loads survey metadata and response rows into a PostgreSQL data warehouse.

The implementation is organized around two jobs: make the RemoteControl endpoints straightforward and safe to call, and transform their results into a relational snapshot that can be queried downstream.

Calling LimeSurvey RemoteControl endpoints

LimeSurvey RemoteControl uses JSON-RPC over HTTP POST, including for read-only operations. The client sends requests to the configured RemoteControl URL with a method name, positional params array, and incrementing request ID. It authenticates first with get_session_key; the resulting key is prepended to every subsequent method’s parameters. When the client closes, it calls release_session_key.

A typical request is shaped like this:

{
  "method": "list_questions",
  "params": ["<session-key>", 12345, null, null],
  "id": 7
}

The Java implementation wraps that protocol in named methods, so callers do not need to assemble JSON-RPC requests themselves. For example:

try (LimeSurveyClient client = LimeSurveyClient.connect(
        URI.create(System.getenv("LIMESURVEY_URL")),
        System.getenv("LIMESURVEY_USERNAME"),
        System.getenv("LIMESURVEY_PASSWORD"))) {
    JsonNode surveys = client.listSurveys(null, null);
    JsonNode questions = client.listQuestions(12345, null, null);
    byte[] export = client.exportResponses(12345, "json", new ExportOptions());
}

Try-with-resources makes session cleanup part of the call lifecycle. Optional parameters are passed as null when the caller wants LimeSurvey’s defaults. The library exposes these retrieval methods:

  • list_surveys for surveys visible to the account, with optional user and survey-group filters.
  • get_survey_properties and get_language_properties for survey configuration.
  • get_fieldmap for the export fields available for a survey.
  • list_groups and get_group_properties for question-group structure and details.
  • list_questions and get_question_properties for question records and details.
  • get_summary for survey statistics.
  • get_response_ids to retrieve response IDs associated with a participant token.
  • export_responses and export_responses_by_token for response data, with export options such as format, language, completion status, heading type, response type, selected fields, and response-ID bounds where supported.

Export calls return base64-encoded content from the API; the client decodes it to bytes so JSON, CSV, and other supported formats are not corrupted. Token-specific calls require a token supplied through the environment rather than a command-line argument.

Reliable endpoint integration needs more than sending a request. The client checks HTTP status, parses JSON, verifies the response ID, surfaces JSON-RPC and LimeSurvey status errors, and rejects absent or malformed results. Export payloads are validated before decoding. Error messages redact session keys, credentials, and participant tokens. It deliberately avoids silent retries or automatic re-authentication, making failures visible to the caller.

From API responses to a warehouse model

The sync command composes the endpoints into a survey-by-survey extraction. It lists accessible surveys, reads each survey’s properties, groups and questions, and retrieves a JSON response export. It then maps response export columns back to question records using question codes, including sub-question codes such as Q002_SQ001 and Q002[SQ001].

The PostgreSQL data warehouse is organized as a dedicated schema with these core tables:

  • survey stores identifiers, title, active state, dates, group, language, and the last synchronization run.
  • question_group and question hold the survey structure, including parent/child question relationships and ordering.
  • response stores one row per response, selected response metadata, completion state, and a filtered JSONB representation of the export.
  • answer stores non-empty answer values by response and export field code, with a question link when the code can be resolved.
  • sync_run records the run status, survey counts, timestamps, and a bounded error summary.

Each survey is handled as a transaction. The sync process upserts its survey row, replaces its existing groups, questions, responses, and answers, and commits the snapshot together. If one survey fails, its transaction rolls back while the remaining surveys continue. This makes reruns idempotent and removes rows for responses that have since been deleted in LimeSurvey. The warehouse holds current state rather than a full history; sync_run provides operational run records.

LimeSurvey’s ERR_NO_DATA and ERR_NO_RESPONSE_TABLE responses are treated as empty lists or exports where appropriate, allowing inactive or unanswered surveys to be represented with their metadata and questions. Other errors are recorded as failures. At completion, the run is marked success, partial, or failed, and an incomplete sync exits with a nonzero status.

Privacy is built into the transformation. Participant tokens, IP addresses, and referrer URLs are removed before response data is written to either the answer rows or the JSONB raw field. Response answers may still contain sensitive information, so warehouse access and retention should be managed accordingly.

Running the client and synchronization

The project is packaged with Maven as an executable JAR. Configure the LimeSurvey endpoint and account through environment variables; supply the password via the environment or the client’s no-echo terminal prompt. For network connections beyond a trusted local environment, use HTTPS.

The CLI has commands for each supported retrieval operation, such as list-surveys, survey-properties, list-questions, summary, response-ids, and the two response export variants. The warehouse sync is invoked separately:

export LIMESURVEY_URL='https://surveys.example.org/index.php/admin/remotecontrol'
export LIMESURVEY_USERNAME='your-username'
export LIMESURVEY_PASSWORD='your-password'
export PG_URL='jdbc:postgresql://localhost:5432/analytics'
export PG_USER='warehouse-user'
# Set PG_PASSWORD securely, or enter it at the terminal prompt.

java -jar target/limesurvey-client-1.0.0.jar sync

The database connection URL must not embed credentials. The sync installs its idempotent schema definition in the dedicated namespace and uses prepared statements for database writes. Its database role therefore needs permission to create that schema and its tables, while all synchronized objects remain isolated from LimeSurvey’s own application tables.

A foundation for analysis

This work turns a collection of API calls into a reusable integration layer: the Java client handles session and protocol details, the CLI makes retrieval and synchronization operational, and the warehouse model preserves survey structure alongside response-level and answer-level data. Because question mapping, error reporting, transactional replacement, and privacy filtering are handled in the pipeline, analysts can query consistent relational rows without having to reconstruct every LimeSurvey export by hand.