rev. 334
services/apiref
services/apisrv
services/caches
services/caches/formatters
services/caches/search
services/caches/shortcuts
services/logs
services/oauth
services/replicate
services/users

Examples and libraries

Here you will find basic examples of OKAPI usage with popular programming languages.

Are there any client libraries?

OKAPI does not require you to use any special libraries, usually you will want to use OKAPI "as is", via basic HTTP requests and responses.

However, some third-party libraries exist and you can use them if you want. With proper libraries, OKAPI might be easier to use. We give you the list of all libraries we know of. It's your choice to decide which are "proper".

  • If you're developing with .NET, you may want to check out .NET client library by Oliver Dietz.
  • (if you've developed your own library and would like to include it here, post the details in a comment thread below)

You should check with the author of the library before you use it, to make sure it is up-to-date. If you believe it is not, then keep in mind that learning to use our REST protocol might be the safest choice.

PHP Example

Example 1. This will print the number of users in the OpenCaching.US installation:

<?

$json = file_get_contents("http://www.opencaching.us/okapi/services/apisrv/stats");
$data = json_decode($json);
print "Number of OpenCaching.US users: ".$data->user_count;

?>

Example 2. This will print the codes of some nearest unfound caches:

<?

/* Enter your OKAPI's URL here. */
$okapi_base_url = "http://opencaching.pl/okapi/";

/* Enter your Consumer Key here. */
$consumer_key = "YOUR_KEY_HERE";

/* Username. Caches found by the given user will be excluded from the results. */
$username = "USERNAME_HERE";

/* Your location. */
$lat = 54.3;
$lon = 22.3;

/* 1. Get the UUID of the user. */
$json = @file_get_contents($okapi_base_url."services/users/by_username".
	"?username=".$username."&fields=uuid&consumer_key=".$consumer_key);
if (!$json)
	die("ERROR! Check your consumer_key and/or username!\n");
$user_uuid = json_decode($json)->uuid;
print "Your UUID: ".$user_uuid."\n";
	
/* 2. Search for caches. */
$json = @file_get_contents($okapi_base_url."services/caches/search/nearest".
	"?center=".$lat."|".$lon."&not_found_by=".$user_uuid."&limit=5".
	"&consumer_key=".$consumer_key);
if (!$json)
	die("ERROR!");
$cache_codes = json_decode($json)->results;

/* Display them. */
print "Five nearest unfound caches: ".implode(", ", $cache_codes)."\n";

?>

Please note that the above examples use very simple error checking routines. If you want to be "professional", you should catch HTTP 400 Responses, read their bodies (OKAPI error messages), and deal with them more gracefully.

JavaScript Example

It is possible to access OKAPI directly from user's browser, without the need for server backend. OKAPI allows Cross-domain XHR requests. You can also use JSONP output format. There are some limitations of both these techniques though.

This example does the following:

  • Pulls the list of all OKAPI installations from one of the OKAPI servers and displays it in a select-box. Note, that this method does not require Consumer Key (Level 0 Authentication).
  • Asks you to share your location (modern browser can do that).
  • Retrieves a list of nearest geocaches. (This time, it uses the Consumer Key you have to supply.)

Run this example

Comments