Ready to get started?

Connect to live data from Harvest with the API Driver

Connect to Harvest

Create a Data Access Object for Harvest Data using JDBI



A brief overview of creating a SQL Object API for Harvest data in JDBI.

JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for Harvest integrates connectivity to live Harvest data in Java applications. By pairing these technologies, you gain simple, programmatic access to Harvest data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read Harvest data.

Create a DAO for the Harvest Invoices Entity

The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.

public interface MyInvoicesDAO { //request specific data from Harvest (String type is used for simplicity) @SqlQuery("SELECT ClientName FROM Invoices WHERE State = :state") String findClientNameByState(@Bind("state") String state); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Harvest

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Harvest.

Start by setting the Profile connection property to the location of the Harvest Profile on disk (e.g. C:\profiles\Harvest.apip). Next, set the ProfileSettings connection property to the connection string for Harvest (see below).

Harvest API Profile Settings

To authenticate to Harvest, you can use either Token authentication or the OAuth standard. Use Basic authentication to connect to your own data. Use OAuth to allow other users to connect to their data.

Using Token Authentication

To use Token Authentication, set the APIKey to your Harvest Personal Access Token in the ProfileSettings connection property. In addition to APIKey, set your AccountId in ProfileSettings to connect.

Using OAuth Authentication

First, register an OAuth2 application with Harvest. The application can be created from the "Developers" section of Harvest ID.

After setting the following connection properties, you are ready to connect:

  • ProfileSettings: Set your AccountId in ProfileSettings.
  • AuthScheme: Set this to OAuth.
  • OAuthClientId: Set this to the client ID that you specified in your app settings.
  • OAuthClientSecret: Set this to the client secret that you specified in your app settings.
  • CallbackURL: Set this to the Redirect URI that you specified in your app settings.
  • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to manage how the driver obtains and refreshes the OAuthAccessToken.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the Harvest JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.api.jar

Fill in the connection properties and copy the connection string to the clipboard.

A connection string for Harvest will typically look like the following:

jdbc:api:Profile=C:\profiles\Harvest.apip;ProfileSettings='APIKey=my_personal_key;AccountId=_your_account_id';

Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.

DBI dbi = new DBI("jdbc:api:Profile=C:\profiles\Harvest.apip;ProfileSettings='APIKey=my_personal_key;AccountId=_your_account_id';"); MyInvoicesDAO dao = dbi.open(MyInvoicesDAO.class); //do stuff with the DAO dao.close();

Read Harvest Data

With the connection open to Harvest, simply call the previously defined method to retrieve data from the Invoices entity in Harvest.

//disply the result of our 'find' method String clientName = dao.findClientNameByState("open"); System.out.println(clientName);

Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Harvest by integrating with the CData JDBC Driver for Harvest. Download a free trial and work with live Harvest data in custom Java applications today.