Custom REST API integration for nonprofit data and charity verification · 2021
The challenge
Before money goes to a nonprofit, somebody has to confirm it is a real one. The official name, the IRS status, whether donations to it are actually tax-deductible. None of that was in Salesforce.
GuideStar, now part of Candid, holds it for almost every nonprofit in the US. My client wanted the check to happen on the Account, where the work already happens, instead of somewhere else.
The catch was time. I only had a trial API key, which meant I had to build and test the whole integration in a sandbox before the trial ran out. No second chances to just check one more response later.
What the user sees
The user opens an Account, enters the EIN and clicks one button in a custom component. A few seconds later the Account has the organization's official name, address and its charity and tax-exempt status, straight from GuideStar.
The EIN, the Employer Identification Number, is the ID every US nonprofit has, so it was the natural key for the search. It also comes in different formats, with or without the dash, so the input has to be cleaned before it goes anywhere.
How the connection works
GuideStar's API used a subscription key rather than OAuth. In 2021 Salesforce only had what we now call legacy Named Credentials. The key lived in the Named Credential and went out in a custom HTTP header with a merge field. That way it never sits in Apex code and nobody reads it off the org. The endpoint also needed a Remote Site Setting.
The Apex side
Salesforce sends the callout, gets JSON back and maps it to the Account fields. JSON from an external system is never as tidy as the documentation promises, so the mapping has to survive empty fields and missing values without breaking the record.
Charity status is a good example of why the details matter. Being tax-exempt with the IRS and being eligible for tax-deductible donations are not the same thing, and GuideStar's data separates the two. Mapping them to one checkbox would have looked fine and been wrong.
Some of the processing runs in asynchronous Apex, so the user isn't waiting on it and the org stays inside its callout limits. Salesforce allows 100 callouts per transaction.
Working against a trial deadline
Before writing any Apex I tested the API in Postman. I wanted to see exactly what GuideStar sends back for different searches, the good ones and the bad ones. Then I used those real JSON responses with HttpCalloutMock in my Apex tests.
That part saved the project. The tests didn't need the live API, so they kept working after the trial key expired. The client had a tested integration without paying for the API just to build it. Mocks also make it easy to test the unhappy paths: an EIN that doesn't exist, an empty response, an expired key. Those you really don't want to meet for the first time in production.
Stack
Apex · REST API · JSON · legacy Named Credentials · Remote Site Settings · asynchronous Apex · Postman · HttpCalloutMock
Need something connected to Salesforce?
This is the kind of work I do most. If you have a system that should be talking to Salesforce and isn't, tell me what it is.