Skip to content

Add configurable Twilio API base URL#22

Open
rnybergh wants to merge 1 commit into
get-convex:mainfrom
rnybergh:twilio-api-base-url
Open

Add configurable Twilio API base URL#22
rnybergh wants to merge 1 commit into
get-convex:mainfrom
rnybergh:twilio-api-base-url

Conversation

@rnybergh

@rnybergh rnybergh commented Mar 7, 2026

Copy link
Copy Markdown

Summary

  • add TWILIO_API_BASE_URL support to the shared Twilio request helper while keeping https://api.twilio.com as the default
  • add tests for default behavior, custom EU endpoints, and trailing slash normalization
  • document the new optional env var in the package README

Test plan

  • npm test
  • npm run build
  • npm run typecheck

Made with Cursor

Summary by CodeRabbit

  • New Features

    • Added support for configurable Twilio API base URL to enable regional routing, with https://api.twilio.com as the default.
  • Documentation

    • Updated README with guidance on setting TWILIO_API_BASE_URL for regional API routing.
  • Tests

    • Added tests for Twilio API utility functions.

Allow regional Twilio REST endpoints to be selected with TWILIO_API_BASE_URL while preserving api.twilio.com as the default.

Made-with: Cursor
@coderabbitai

coderabbitai Bot commented Mar 7, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Adds configurable Twilio API base URL support via environment variable TWILIO_API_BASE_URL, defaulting to https://api.twilio.com. Introduces helper functions to resolve the base URL and construct request URLs. Refactors twilioRequest to use these utilities. Includes comprehensive test coverage.

Changes

Cohort / File(s) Summary
Documentation
README.md
Added guidance for optional TWILIO_API_BASE_URL environment variable configuration with default value reference.
Core Implementation
src/component/utils.ts
Introduced resolveTwilioApiBaseUrl() to read and resolve API base URL from environment with fallback default. Added buildTwilioRequestUrl() to construct request URLs using resolved base. Refactored twilioRequest() to use new URL builder function.
Test Coverage
src/component/utils.test.ts
Added comprehensive tests covering resolveTwilioApiBaseUrl (default and custom values, trailing slash handling), buildTwilioRequestUrl (URL construction), and twilioRequest (integration with URL resolution and fetch mocking).

Sequence Diagram

sequenceDiagram
    participant App as Application
    participant TRequest as twilioRequest()
    participant BuildUrl as buildTwilioRequestUrl()
    participant Resolve as resolveTwilioApiBaseUrl()
    participant Env as Environment
    participant HTTP as HTTP Client

    App->>TRequest: Make Twilio request
    TRequest->>BuildUrl: Build request URL
    BuildUrl->>Resolve: Get API base URL
    Resolve->>Env: Read TWILIO_API_BASE_URL
    alt Env var set
        Env-->>Resolve: Return configured URL
    else Env var not set
        Resolve-->>Resolve: Use default https://api.twilio.com
    end
    Resolve-->>BuildUrl: Return base URL (trimmed)
    BuildUrl-->>BuildUrl: Append /2010-04-01/Accounts/{SID}/{resource}.json
    BuildUrl-->>TRequest: Return constructed URL
    TRequest->>HTTP: POST request
    HTTP-->>TRequest: Response
    TRequest-->>App: Return result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Wiggles nose with delight
A rabbit hops through config lands so bright,
Base URLs bend to our regional will,
Tests verify each twilio call and skill,
No more hardcoded paths holding us down,
Environment variables—the finest in town! 🌍✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add configurable Twilio API base URL' accurately summarizes the main change: introducing support for configurable API base URLs via environment variable.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/component/utils.test.ts (1)

37-45: Consider adding a test case for default base URL.

The current test only covers the custom base URL scenario. Adding a test without TWILIO_API_BASE_URL set would verify the default URL construction as well.

🧪 Suggested additional test case
 describe("buildTwilioRequestUrl", () => {
+  it("builds the REST URL from the default base URL", () => {
+    expect(buildTwilioRequestUrl("Messages.json", ACCOUNT_SID)).toBe(
+      "https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json",
+    );
+  });
+
   it("builds the REST URL from the configured base URL", () => {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/component/utils.test.ts` around lines 37 - 45, Add a new test for
buildTwilioRequestUrl that verifies behavior when TWILIO_API_BASE_URL is not
set: clear or delete process.env.TWILIO_API_BASE_URL, call
buildTwilioRequestUrl("Messages.json", ACCOUNT_SID) and assert it returns the
default Twilio base URL form (e.g.
"https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json"); reference
buildTwilioRequestUrl and ACCOUNT_SID to locate where to add the test and ensure
you restore or not modify other env vars across tests to avoid cross-test
pollution.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/component/utils.test.ts`:
- Around line 37-45: Add a new test for buildTwilioRequestUrl that verifies
behavior when TWILIO_API_BASE_URL is not set: clear or delete
process.env.TWILIO_API_BASE_URL, call buildTwilioRequestUrl("Messages.json",
ACCOUNT_SID) and assert it returns the default Twilio base URL form (e.g.
"https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json"); reference
buildTwilioRequestUrl and ACCOUNT_SID to locate where to add the test and ensure
you restore or not modify other env vars across tests to avoid cross-test
pollution.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d0e06eca-3d5a-451f-8172-7b7a306405e8

📥 Commits

Reviewing files that changed from the base of the PR and between 57ade5c and d5a37c7.

📒 Files selected for processing (3)
  • README.md
  • src/component/utils.test.ts
  • src/component/utils.ts

@rnybergh

rnybergh commented Mar 7, 2026

Copy link
Copy Markdown
Author

I noticed that Twilio does not seem to support message sending using the EU endpoints. But having the endpoint configurable using an env variable is still valuable, although the original use-case (EU) is not currently possible..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant