# Central API examples

**URL:** <https://forum.getodk.org/t/central-api-examples/32335>\
**Category:** Development\
**Tags:** odk-central\
**Created:** [February 5, 2021, 9:13am UTC](https://forum.getodk.org/t/central-api-examples/32335 "2021-02-05T09:13:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aurdipas](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/aurdipas/32/1785_2.png) [@aurdipas](https://forum.getodk.org/u/aurdipas)\
**Post date:** [February 5, 2021, 9:13am UTC](https://forum.getodk.org/t/central-api-examples/32335/1 "2021-02-05T09:13:28Z")

</div>

Hi,

I had to give some support to folk that wanted to use the ODK Central API to extract data.

I think it would be useful to have some examples for user not so experienced.

Examples for example on how to create a java method to download csv or zip etc...

**Example csv**

```
public static void main(String[] args) {
	Client client = ClientBuilder.newClient();
	Response response = client.target("https://*MYCENTRAL*/v1/projects/*projectId*/forms/*xmlFormId*/submissions.csv")
			.request(MediaType.TEXT_PLAIN_TYPE).
			header("Authorization", "Bearer *AUTHTOKEN*").
			get();

	File downloadedFile = new File("\\*PATHTOSAVE*\\submission.csv");
	String str = response.readEntity(String.class);
    BufferedWriter writer;
	try {
		writer = new BufferedWriter(new FileWriter(downloadedFile));
	    writer.write(str);
	    
	    writer.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}

```

**Example zip**

```
public static void main(String[] args) {
	InputStream inputStream = null;
	OutputStream outputStream = null;
    int responseCode;
    String responseMessageFromServer = null;
    String filePath = null;
	// TODO Auto-generated method stub
	Client client = ClientBuilder.newClient();
	Response response = client.target("https://*MYCENTRAL*/v1/projects/*projectId*/forms/*xmlFormId*/submissions.csv.zip?media=true&%24filter= __system%2FsubmissionDate%20gt%202021-01-01%20and%20__ system%2FsubmissionDate%20lt%202021-01-25")
			.request(MediaType.TEXT_PLAIN_TYPE).
			header("Authorization", "Bearer *AUTHTOKEN*").
			get();

	responseCode = response.getStatus();

    System.out.println("Response code: " + responseCode);

    if (response.getStatus() != 200) {
       throw new RuntimeException("Failed with HTTP error code : " + responseCode);
    }
	
    // get response message
    responseMessageFromServer = response.getStatusInfo().getReasonPhrase();
    System.out.println("ResponseMessageFromServer: " + responseMessageFromServer);
    // read response
	inputStream = response.readEntity(InputStream.class);
	try {
		filePath = "\\*PATHTOSAVE*\\submission.zip";
        outputStream = new FileOutputStream(filePath);
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
             outputStream.write(buffer, 0, bytesRead);
        }
        
        System.out.println("downloaded successfully at " + filePath);
      	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

```

Maybe some links to OData [Built-in Filter Operations](http://docs.oasis-open.org/odata/odata/v4.01/cs01/part1-protocol/odata-v4.01-cs01-part1-protocol.html#sec_BuiltinFilterOperations) or [Built-in Query Functions](http://docs.oasis-open.org/odata/odata/v4.01/cs01/part1-protocol/odata-v4.01-cs01-part1-protocol.html#sec_BuiltinQueryFunctions) directly on the page of [export submissions](https://odkcentral.docs.apiary.io/#reference/forms-and-submissions/submissions/exporting-form-submissions-to-csv) and some practical examples on filters:

e.g. I want to filter submission between 2 dates "filter=\_\_system%2FsubmissionDate%20gt%202021-01-01%20and%20\_\_system%2FsubmissionDate%20lt%202021-01-31"

Do you think this kind of info could be directly on [https://odkcentral.docs.apiary.io/#](https://odkcentral.docs.apiary.io/#) or maybe on the forum?

---

<div class="post-metadata">

**Author:** ![mathieubossaert](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/mathieubossaert/32/6131_2.png) [@mathieubossaert](https://forum.getodk.org/u/mathieubossaert)\
**Post date:** [March 16, 2021, 11:10am UTC](https://forum.getodk.org/t/central-api-examples/32335/2 "2021-03-16T11:10:04Z")

</div>

> [@aurdipas](#):
>
> "filter=\_\_system%2FsubmissionDate%20gt%202021-01-01%20and%20\_\_system%2FsubmissionDate%20lt%202021-01-31"

Thanks @aurdipas, it helped me a lot to integrate a date filter to my pgsql-http requests !

---

<div class="post-metadata">

**Author:** ![Matthew\_White](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/matthew_white/32/1654_2.png) [@Matthew\_White](https://forum.getodk.org/u/Matthew_White)\
**Post date:** [March 16, 2021, 1:26pm UTC](https://forum.getodk.org/t/central-api-examples/32335/3 "2021-03-16T13:26:34Z")

</div>

Thanks, @aurdipas!

I think it's a great idea to add links and and an example about `$filter` to the API docs. I've created a GitHub issue [here](https://github.com/getodk/central-backend/issues/338) to discuss that further.

For examples of API requests in Java or another language, I think the showcase is a great place.

Relatedly, I have noticed that Apiary includes examples of API requests in different languages. (If you click on an endpoint, then below the attributes list, you can switch from Raw to a language of your choice.) That said, even within a single language, there are multiple ways of sending a request, and I'm not sure that Apiary shows the easiest or most modern way to do so. And as your example shows, some API requests, like downloading a file, are more complicated than others.

---

<div class="post-metadata">

**Author:** ![aurdipas](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/aurdipas/32/1785_2.png) [@aurdipas](https://forum.getodk.org/u/aurdipas)\
**Post date:** [April 29, 2021, 4:22pm UTC](https://forum.getodk.org/t/central-api-examples/32335/4 "2021-04-29T16:22:08Z")

</div>

Small other example when you need to filter for datetime (including hours,minutes and seconds).  
To add the Hour minute and second the format is:

THH%3Amm%3AssZ

where the %3A is the colon encoded, T indicates the time and Z the zone.

```
Client client = ClientBuilder.newClient();
Response response = client.target("https://*MYCENTRAL*/v1/projects/*projectId*/forms/*xmlFormId*/submissions.csv.zip?media=true&%24filter= __system%2FsubmissionDate%20gt%202021-01-01T14%3A00%3A00Z%20and%20__ system%2FsubmissionDate%20lt%202021-01-25T18%3A00%3A00Z")
		.request(MediaType.TEXT_PLAIN_TYPE).
		header("Authorization", "Bearer *AUTHTOKEN*").
		get();

```

---

<div class="post-metadata">

**Author:** ![rclazarito](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/rclazarito/32/16050_2.png) [@rclazarito](https://forum.getodk.org/u/rclazarito)\
**Post date:** [December 29, 2021, 5:58am UTC](https://forum.getodk.org/t/central-api-examples/32335/5 "2021-12-29T05:58:01Z")

</div>

Hi Aurdipas,

Do you have an example on updating the submissions metadata? Tried the samples from [API docs](https://odkcentral.docs.apiary.io/reference/submissions/submissions/updating-submission-metadata) but nothing happens. It is almost the same as the Retrieving Submission metadata.

Thanks.

Raymund
