Created
September 20, 2025 22:51
-
-
Save ctala/471e7f63a925da8f717ab2922f307698 to your computer and use it in GitHub Desktop.
A practical test suite and getting-started guide for the Unipile Node.js SDK. This script demonstrates how to authenticate and fetch various resources from the Unipile API, with a focus on LinkedIn data like posts, comments, and user profiles. It includes working examples, environment setup with `dotenv`, and detailed documentation on how to run…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * MIT License | |
| * | |
| * Copyright (c) 2025 Cristian Tala Sánchez | |
| * | |
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| * of this software and associated documentation files (the "Software"), to deal | |
| * in the Software without restriction, including without limitation the rights | |
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| * copies of the Software, and to permit persons to whom the Software is | |
| * furnished to do so, subject to the following conditions: | |
| * | |
| * The above copyright notice and this permission notice shall be included in all | |
| * copies or substantial portions of the Software. | |
| * | |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| * SOFTWARE. | |
| */ | |
| /* | |
| * ============================================================================= | |
| * ABOUT THIS SCRIPT | |
| * ============================================================================= | |
| * | |
| * **What is Unipile?** | |
| * Unipile offers a powerful Unified API that allows developers to seamlessly | |
| * integrate multiple communication channels (like LinkedIn, Email, WhatsApp, etc.) | |
| * into their applications. Instead of building and maintaining separate integrations | |
| * for each platform, you can use the Unipile API as a single, consistent interface, | |
| * saving time and complexity. | |
| * | |
| * **Why is this Test Suite Important?** | |
| * This script serves as a practical, hands-on guide for developers getting started | |
| * with the Unipile Node.js SDK. It provides a working set of examples that demonstrate | |
| * key, real-world operations such as fetching posts, comments, user profiles, and more. | |
| * By running these tests, you can quickly: | |
| * - Verify your environment and authentication setup. | |
| * - Understand the structure of the data returned by the API. | |
| * - See concrete examples of how to call different API endpoints. | |
| * - Learn important distinctions, like when to use `social_id` versus the | |
| * standard `id` for certain operations. | |
| * | |
| * Think of this file as a live tutorial to jumpstart your Unipile integration. | |
| * | |
| * ============================================================================= | |
| */ | |
| /* | |
| * ============================================================================= | |
| * HOW TO USE THIS SCRIPT FROM SCRATCH | |
| * ============================================================================= | |
| * | |
| * This script is designed to be run in a Node.js environment. | |
| * | |
| * 1. **Setup Your Project:** | |
| * - Create a new folder for your project. | |
| * - Save this code as `index.js` inside that folder. | |
| * - Open your terminal in the project folder. | |
| * | |
| * 2. **Initialize Node.js Project:** | |
| * Run the following command to create a `package.json` file: | |
| * `npm init -y` | |
| * | |
| * 3. **Set Project Type:** | |
| * Open the newly created `package.json` file and add the following line inside | |
| * the main curly braces to enable ES Module syntax: | |
| * `"type": "module",` | |
| * | |
| * 4. **Install Dependencies:** | |
| * Run the following command to install the necessary packages: | |
| * `npm install unipile-node-sdk dotenv` | |
| * | |
| * 5. **Create .env file:** | |
| * Create a file named `.env` in the same directory and add your sensitive data: | |
| * ACCESS_TOKEN="your_unipile_access_token_here" | |
| * ACCOUNT_ID="your_unipile_account_id_here" | |
| * POST_ID="the_post_id_you_want_to_test_with" | |
| * | |
| * 6. **Run the script:** | |
| * Execute the script with: | |
| * `node index.js` | |
| * | |
| * 7. **Important Note on Post IDs:** | |
| * This script first fetches a post (Test 1) and then uses the `social_id` | |
| * from that post's response to fetch its comments (Test 2). Using the | |
| * regular `id` for fetching comments may result in an error. This script | |
| * handles this logic automatically. | |
| * | |
| * ============================================================================= | |
| */ | |
| import 'dotenv/config'; | |
| import { UnipileClient } from "unipile-node-sdk" | |
| // SDK setup | |
| const BASE_URL = "https://api18.unipile.com:14891" | |
| const ACCESS_TOKEN = process.env.ACCESS_TOKEN; | |
| // --- Test Data --- | |
| const account_id = process.env.ACCOUNT_ID; | |
| const post_id = process.env.POST_ID; | |
| const user_provider_id = "ACoAAAEWDgIBhyU--VWuLdcbULkRL2DmPjsREuQ" | |
| const company_provider_id = "Unipile" // This one might work as-is | |
| const client = new UnipileClient(BASE_URL, ACCESS_TOKEN) | |
| async function runTests() { | |
| console.log("--- Running Unipile SDK Tests ---"); | |
| let social_id_from_test1; | |
| try { | |
| console.log("\n[1/6] Retrieving a LinkedIn post..."); | |
| const post = await client.users.getPost({ | |
| account_id, | |
| post_id, | |
| }); | |
| console.log("✅ Success:", post); | |
| if (post && post.social_id) { | |
| social_id_from_test1 = post.social_id; | |
| console.log(` -> Stored social_id for next test: ${social_id_from_test1}`); | |
| } | |
| } catch (error) { | |
| console.error("❌ Error:", error.body || error.message); | |
| } | |
| if (social_id_from_test1) { | |
| try { | |
| console.log("\n[2/6] Listing LinkedIn Post Comments..."); | |
| const comments = await client.users.getAllPostComments({ | |
| account_id, | |
| post_id: social_id_from_test1, // Using social_id from test 1 | |
| }); | |
| console.log("✅ Success:", comments); | |
| } catch (error) { | |
| console.error("❌ Error:", error.body || error.message); | |
| } | |
| } else { | |
| console.log("\n[2/6] Listing LinkedIn Post Comments... SKIPPED (Could not get social_id from test 1)"); | |
| } | |
| try { | |
| console.log("\n[3/6] Listing contacts/relations..."); | |
| const relations = await client.users.getAllRelations({ account_id }); | |
| console.log("✅ Success:", relations); | |
| } catch (error) { | |
| console.error("❌ Error:", error.body || error.message); | |
| } | |
| try { | |
| console.log("\n[4/6] Retrieving a Company Profile..."); | |
| const companyProfile = await client.users.getCompanyProfile({ | |
| account_id: account_id, | |
| identifier: company_provider_id, | |
| }); | |
| console.log("✅ Success:", companyProfile); | |
| } catch (error) { | |
| console.error("❌ Error:", error.body || error.message); | |
| } | |
| try { | |
| console.log("\n[5/6] Listing a User's Posts..."); | |
| const userPosts = await client.users.getAllPosts({ | |
| account_id: account_id, | |
| identifier: user_provider_id, | |
| }); | |
| console.log("✅ Success:", userPosts); | |
| } catch (error) { | |
| console.error("❌ Error:", error.body || error.message); | |
| } | |
| try { | |
| console.log("\n[6/6] Retrieving a User Profile..."); | |
| const userProfile = await client.users.getProfile({ | |
| account_id: account_id, | |
| identifier: user_provider_id, | |
| }); | |
| console.log("✅ Success:", userProfile); | |
| } catch (error) { | |
| console.error("❌ Error:", error.body || error.message); | |
| } | |
| console.log("\n--- Tests Finished ---"); | |
| } | |
| runTests(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment