GuideStar API Sandbox - Code Examples

Document created by communitymanager Administrator on Feb 26, 2016Last modified by JackCowardin on Apr 4, 2017
Version 6Show Document
  • View in full screen mode

Example Data Classes for API Return Data

Use these classes to deserialize API return data.

 

Test an API URL in a Browser

GuideStar APIs can be tested by entering the URL in the address bar of any Browser. A prompt will be displayed to enter the user Name (or API Key) and password :

 

sandbox-browser.PNG

 

cURL API Example

cURL is a program that provides a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997. The name originally stood for "see URL". cURL - Wikipedia

curl -3 -u username:password https://sandboxdata.guidestar.org/v1_1/search?q=ein:54-1774039 

 

 

C# .NET API Example      (Text version for copying the example is here: C# .NET Code Example )

Sandbox-Search.PNG

 

 

Java API Example

This example makes use of the Apache HttpClient library 4.1 to simplify http requests. To use this library, download the binaries with dependencies from http://hc.apache.org/ and add them to your project classpath. Include your GuideStar credentials in place of the user:password.

 package org.guidestar.data.api;

 

 

 

import java.io.IOException;

import org.apache.http.HttpEntity;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import org.apache.commons.codec.binary.Base64;

 

public class TestAPI

{

  //this is the request string that calls the API. The Sandbox API is being used for this example.

  //Search for Organizations in the 23188 zip code and return 25 results.

  private static String request = "https://sandboxdata.guidestar.org/v1/quickstartsearch?q=zip:23188&r=25"; 

  public static void main(String[] args) throws ClientProtocolException, IOException

  {

       //encode login credentials

       byte[] encodedBytes = Base64.encodeBase64(<username> + ":" + <password>.getBytes()); 

       CloseableHttpClient httpclient = HttpClients.createDefault();

       HttpGet httpget = new HttpGet(request); 

       httpget.setHeader("Authorization","Basic " + new String(encodedBytes));  //Set the authorization header with the GuideStar API credentials

       CloseableHttpResponse response = httpclient.execute(httpget); 

 

      try //We want to try and get an entity back from the response to our request

       {

            HttpEntity entity = response.getEntity(); 

            if (entity != null)

            {

                 System.out.println(EntityUtils.toString(entity));

            }

       }

       catch(Exception e)

      {

            e.printStackTrace();

       }

       finally //close the response object and client object

      {    

            response.close();

            httpclient.close();

       }

    }

}

1 person found this helpful

Attachments

    Outcomes