Pennsylvania Department of State (United States)
reference Facts, options, APIs
On this page
- Open data, not the live register
- Active registrations only
- Settings
- Vocabulary and Properties
- Request and response
- Properties added by the enricher
- How the script handles input and errors
- Testing the enricher
- Limitations
- Process script
This example looks up an organization’s Pennsylvania filing number in the business registrations of the Pennsylvania Department of State (Bureau of Corporations and Charitable Organizations). It uses the extract of active registrations that the Department publishes on OpenDataPA (dataset xvd7-5r2c, Registered Businesses in PA Current by County Department of State). For each golden record it stores the business name, type of registration, creation date, address and county. No API key is needed.
Enricher version: V2
API key: not needed (an optional data.pa.gov app token raises the rate limit)
Open data, not the live register
The dataset is an extract of the Department of State’s registration system that the portal refreshes monthly (its metadata says Update Frequency: Monthly). It isn’t a live query of the register, so a recent filing may take a month or more to appear. For the current state of a business, use the Department of State’s business search.
Active registrations only
The dataset lists only active business registrations. A business that has been dissolved, withdrawn or otherwise made inactive isn’t in it, so it gets no result.
To set up the enricher, see Add REST API enricher, then use the settings and script on this page.
Settings
-
Accepted Business Domain –
/Organization, or the business domain that holds your companies. -
Version – V2 (recommended).
-
API Key – leave empty, or enter a data.pa.gov app token. Without a token, data.pa.gov limits how many requests it accepts from one address; with one, the limit is much higher. To get a token, sign in to data.pa.gov and create an app token in your developer settings (see Socrata app tokens). The script sends it in the
X-App-Tokenheader. -
Filters – we recommend a rule that only accepts golden records where
organization.pennsylvaniaFilingNumberhas a value. Otherwise, every record without a filing number is listed on the enricher’s Logs tab as having returned no results.
Vocabulary and Properties
organization.pennsylvaniaFilingNumber
organization.pennsylvaniaFilingNumber holds the filing number (also called the entity number) that the Department of State assigns to each business, with or without its leading zeros, for example 0000159516 or 159516. If your data uses another vocabulary key, change this field and the FILING_NUMBER_KEY value at the top of the script.
Request and response
Base URL: https://data.pa.gov/resource
Endpoint: GET /xvd7-5r2c.json?filing_number={filingNumber}&$select={columns}&$group={columns}&$limit=5
Authentication: none; optional app token in the X-App-Token header
Rate limits: requests without an app token are throttled per IP address; requests with one aren’t, unless they are abusive (see Socrata app tokens). Each request takes about 1–3 seconds.
The dataset has one row for each governor or principal officer of a business (32 rows for The Hershey Company), with the business’s details repeated on each row. So that the response stays small, the script asks only for the business’s own columns, grouped, and the API returns one row per business:
GET https://data.pa.gov/resource/xvd7-5r2c.json?filing_number=0000159516&$select=business_name_source_,filing_number,address_line1,address_line2,city,state,zip,typeofbusinessregistration,creationdate,shortcountyname,county_code&$group=business_name_source_,filing_number,address_line1,address_line2,city,state,zip,typeofbusinessregistration,creationdate,shortcountyname,county_code&$limit=5
Accept: application/json
Response:
[
{
"business_name_source_": "THE HERSHEY COMPANY",
"filing_number": "0000159516",
"address_line1": "600 North Second Street, Suite 401",
"city": "Harrisburg",
"state": "PA",
"zip": "17101",
"typeofbusinessregistration": "Foreign Business Corporation",
"creationdate": "1927-11-02T00:00:00.000",
"shortcountyname": "Dauphin",
"county_code": "22"
}
]
An empty array ([]) means that the filing number isn’t in the dataset: the registration is inactive or the number doesn’t exist. Fields without a value are left out of the row.
Properties added by the enricher
| Property | Response field | Example |
|---|---|---|
pennsylvania.filingNumber | filing_number | 0000159516 |
pennsylvania.name | business_name_source_ (Business Name (Source), as stored in the registration system) | THE HERSHEY COMPANY |
pennsylvania.registrationType | typeofbusinessregistration | Foreign Business Corporation |
pennsylvania.creationDate | creationdate, as YYYY-MM-DD | 1927-11-02 |
pennsylvania.address | address_line1, address_line2, city, state, zip, on one line | 600 North Second Street, Suite 401, Harrisburg, PA 17101 |
pennsylvania.city | city | Harrisburg |
pennsylvania.state | state | PA |
pennsylvania.postalCode | zip | 17101 |
pennsylvania.county | shortcountyname | Dauphin |
pennsylvania.countyCode | county_code, as 2 digits (01–67, in alphabetical order of county) | 22 |
A property is added only when the dataset has a value for it. The dataset’s title-cased Business Name Formatted column (business_name) and its map location columns aren’t added.
If the enricher has a Preview tab, the results appear there first. To add them to golden records, go to the Map tab, select Map Data, and choose Auto Mapping. Select the business domain, enter a new vocabulary named Pennsylvania with the key prefix pennsylvania, and then create the mapping. Auto mapping also uses name as the golden record name, so remove that from the mapping if you want to keep your existing names. Then process the data on the Process tab. If the enricher has no Preview tab, the properties are added to the golden record as soon as it is enriched.
How the script handles input and errors
-
Spaces, dots and dashes are removed, and numbers that lost their leading zeros are padded to 10 digits, so
159516is looked up as0000159516. Numbers that have more than 10 digits, contain letters, or are all zeros are skipped without calling the API. -
If the grouped response has more than one row for the filing number (its details differ between officer rows), the script uses the first one when they all have the same name. If the names differ, it can’t tell which is right; it logs that and adds nothing.
-
If the filing number isn’t in the dataset (because the registration is inactive or the number doesn’t exist), the request is rejected, or the API returns an error, the script logs the reason and adds nothing, and nothing is cached.
-
If you entered an app token and data.pa.gov rejects it, the script throws an error that says so, and the golden record fails.
-
If data.pa.gov limits the requests (HTTP 429), the script throws an error that makes the enricher try the golden record again later. Enter an app token if this happens often.
-
Successful results are cached for 24 hours per filing number. To change this, edit
CACHE_MILLISECONDSin the script.
Testing the enricher
-
Test connection checks only that the script runs. It doesn’t call the API.
-
To try the setup, create a golden record with
organization.pennsylvaniaFilingNumberset to0000159516(The Hershey Company) or0003039985(Comcast Corporation).
Limitations
-
Only active registrations are in the dataset (see Active registrations only), so there is no status property.
-
The dataset is refreshed monthly, not a live query of the register (see Open data, not the live register).
-
Lookups by name aren’t supported.
-
Officers (the dataset’s governor or principal officer columns) aren’t added. To get them, query the same dataset with
$select=party_type,first_name,middle_name,last_name. -
The address is the one in the registration, which for many businesses is a registered office provider’s address, not the business’s own office.
Process script
// REST API enricher (V2) - Pennsylvania business lookup
//
// Looks up a golden record's Pennsylvania filing number in the business registrations of the
// Pennsylvania Department of State, using the extract of active registrations that is published
// on OpenDataPA (https://data.pa.gov/, dataset xvd7-5r2c, "Registered Businesses in PA Current by
// County Department of State"), and adds the result to the golden record as pennsylvania.*
// properties:
//
// pennsylvania.filingNumber filing number that was looked up (10 digits)
// pennsylvania.name business name, as stored in the registration system
// pennsylvania.registrationType type of business registration, for example Domestic Limited Liability Company
// pennsylvania.creationDate date the business was created (YYYY-MM-DD)
// pennsylvania.address registered address, on one line
// pennsylvania.city registered address city
// pennsylvania.state registered address state
// pennsylvania.postalCode registered address ZIP code
// pennsylvania.county county of the registered address
// pennsylvania.countyCode 2-digit county code (01-67, in alphabetical order)
//
// The dataset has one row for each governor or principal officer of a business, with the
// business's details repeated on each row. The script asks only for the business's details,
// grouped, so the API returns one row per business however many officers it has. The dataset
// lists active registrations only; an inactive business is not in it and gets no result.
//
// No API key is needed. An optional Socrata app token (for higher rate limits) can be entered
// in the API Key field. See README.md next to this file for the enricher settings.
//
// A record gets no result (nothing is stored) when it has no valid filing number, when the
// filing number isn't in the dataset, or when the API can't answer.
// ---- Settings -------------------------------------------------------------------------
// Vocabulary key to read. Add the same key to the Vocabulary and Properties field.
const FILING_NUMBER_KEY = "organization.pennsylvaniaFilingNumber";
// The enricher replaces {APIKey} with the API Key field value: an optional data.pa.gov
// (Socrata) app token. Leave the field empty to call the API without one.
const APP_TOKEN = "{APIKey}";
const PENNSYLVANIA_URL = "https://data.pa.gov/resource/xvd7-5r2c.json";
// How long to reuse an answer for the same filing number. 0 turns caching off.
const CACHE_MILLISECONDS = 24 * 60 * 60 * 1000;
// ---- Script ---------------------------------------------------------------------------
// The business's own columns. The officer columns (party_type, first_name, middle_name,
// last_name) are left out, so grouping by these returns one row per business.
const COLUMNS = "business_name_source_,filing_number,address_line1,address_line2,city,state,zip," +
"typeofbusinessregistration,creationdate,shortcountyname,county_code";
const MAX_ROWS = 5;
function getValue(key) {
// A plain loop rather than vocabularies.find(...): vocabularies is a .NET list, and when
// .find is called from inside a function, later calls return the first call's match.
const items = vocabularies || [];
for (let i = 0; i < items.length; i++) {
if (items[i].Key === key) {
const value = items[i].Value;
return value === null || value === undefined ? "" : String(value).trim();
}
}
return "";
}
// Filing numbers are 10 digits with leading zeros, for example 0000159516. Spaces, dashes and
// dots are removed, and numbers that lost their leading zeros are padded. Returns the number or
// null.
function parseFilingNumber(raw) {
const text = raw.replace(/[\s.\-]/g, "");
if (!/^\d{1,10}$/.test(text) || /^0+$/.test(text)) {
return null;
}
return ("0000000000" + text).slice(-10);
}
function clean(value) {
if (value === null || value === undefined) {
return null;
}
const text = String(value).replace(/\s+/g, " ").trim();
return text === "" ? null : text;
}
function parseJson(text) {
try {
return JSON.parse(text);
} catch (e) {
return null;
}
}
// Socrata dates look like "1927-11-02T00:00:00.000". Only the date is kept.
function formatDate(value) {
const text = clean(value);
return text && /^\d{4}-\d{2}-\d{2}/.test(text) ? text.substring(0, 10) : null;
}
// Joins address parts on one line, for example "600 North Second Street, Suite 401, Harrisburg, PA 17101".
function formatAddress(row) {
const stateZip = [clean(row.state), clean(row.zip)].filter((x) => x).join(" ");
const parts = [clean(row.address_line1), clean(row.address_line2), clean(row.city), stateZip].filter((x) => x);
return parts.length > 0 ? parts.join(", ") : null;
}
// Chooses the business's row, or null when there's none or the rows can't be told apart.
function chooseRow(rows, filingNumber) {
const candidates = rows.filter((row) => row && clean(row.filing_number) === filingNumber);
if (candidates.length === 0) {
log(`Pennsylvania registered businesses has no business with filing number ${filingNumber}. It may be inactive or not exist.`);
return null;
}
const best = candidates[0];
const other = candidates.filter((row) => clean(row.business_name_source_) !== clean(best.business_name_source_));
if (other.length > 0) {
log(`Pennsylvania registered businesses has rows under different names for filing number ${filingNumber} (${clean(best.business_name_source_)}, ${clean(other[0].business_name_source_)}). Nothing was added.`);
return null;
}
return best;
}
// Calls the API. Returns the business's rows (possibly none), or null when the API can't answer.
function lookUp(filingNumber) {
const headers = [{ Key: "Accept", Value: "application/json" }];
if (clean(APP_TOKEN)) {
headers.push({ Key: "X-App-Token", Value: APP_TOKEN.trim() });
}
const apiResponse = http.send({
url: `${PENNSYLVANIA_URL}?filing_number=${filingNumber}&$select=${COLUMNS}&$group=${COLUMNS}&$limit=${MAX_ROWS}`,
method: "GET",
headers: headers,
});
const status = String(apiResponse.HttpStatus || "");
const body = parseJson(apiResponse.Content);
if (status === "TooManyRequests") {
throw new Error("TooManyRequests - data.pa.gov is throttling requests, the record will be retried");
}
if ((status === "Forbidden" || status === "Unauthorized") && clean(APP_TOKEN)) {
throw new Error(`data.pa.gov rejected the app token in the API Key field (HTTP ${status}). ${body && body.message ? body.message : ""}`);
}
if (status === "OK" && Array.isArray(body)) {
return body;
}
log(`Pennsylvania registered businesses could not look up filing number ${filingNumber}. HTTP ${status}. ${String(apiResponse.Content || "").substring(0, 200)}`);
return null;
}
function run() {
const filingNumber = parseFilingNumber(getValue(FILING_NUMBER_KEY));
if (!filingNumber) {
// No valid filing number on this record (this is also what Test connection hits, because it
// passes placeholder values instead of real ones).
return [];
}
const cacheKey = `pennsylvania:${filingNumber}`;
const cached = CACHE_MILLISECONDS > 0 ? cache.get(cacheKey) : null;
let row = cached ? parseJson(String(cached)) : null;
if (!row) {
const rows = lookUp(filingNumber);
if (!rows) {
return [];
}
row = chooseRow(rows, filingNumber);
if (!row) {
return [];
}
if (CACHE_MILLISECONDS > 0) {
cache.set(cacheKey, JSON.stringify(row), CACHE_MILLISECONDS);
}
}
const data = {
"pennsylvania.filingNumber": filingNumber,
"pennsylvania.name": clean(row.business_name_source_),
"pennsylvania.registrationType": clean(row.typeofbusinessregistration),
"pennsylvania.creationDate": formatDate(row.creationdate),
"pennsylvania.address": formatAddress(row),
"pennsylvania.city": clean(row.city),
"pennsylvania.state": clean(row.state),
"pennsylvania.postalCode": clean(row.zip),
"pennsylvania.county": clean(row.shortcountyname),
"pennsylvania.countyCode": clean(row.county_code) ? ("0" + clean(row.county_code)).slice(-2) : null,
};
// Leave out values the register didn't return, so they don't overwrite existing data with blanks.
Object.keys(data).forEach((key) => {
if (data[key] === null) {
delete data[key];
}
});
return [{ Data: data, Score: 100 }];
}
response = JSON.stringify(run());