From 781c29f07356752c1efb9e01778a985d2a1d2741 Mon Sep 17 00:00:00 2001 From: Michael Warkentin Date: Fri, 15 May 2026 16:46:18 -0400 Subject: [PATCH] feat(api): Auto-fill org and project slugs in API curl examples When a user is logged in, replace path parameter placeholders in curl examples with real values from their selected project context: - {organization_id_or_slug} / {organization_slug} -> ORG_SLUG - {project_id_or_slug} / {project_slug} -> PROJECT_SLUG Respects the shared project selection dropdown so switching projects updates all API examples. Falls back to the original placeholders when not authenticated. Co-Authored-By: Claude Co-authored-by: Cursor --- src/components/apiExamples/apiExamples.tsx | 34 ++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/components/apiExamples/apiExamples.tsx b/src/components/apiExamples/apiExamples.tsx index da5ab0cc36694a..70939234a5387a 100644 --- a/src/components/apiExamples/apiExamples.tsx +++ b/src/components/apiExamples/apiExamples.tsx @@ -40,6 +40,22 @@ function detectRegionFromApiUrl(apiUrl: string): string | undefined { return match?.[1]; } +type ProjectContext = { + ORG_SLUG: string; + PROJECT_SLUG: string; +}; + +function resolveApiPath(apiPath: string, project: ProjectContext | undefined): string { + if (!project) { + return apiPath; + } + return apiPath + .replace('{organization_id_or_slug}', project.ORG_SLUG) + .replace('{organization_slug}', project.ORG_SLUG) + .replace('{project_id_or_slug}', project.PROJECT_SLUG) + .replace('{project_slug}', project.PROJECT_SLUG); +} + type Props = { api: API; }; @@ -49,10 +65,17 @@ export function ApiExamples({api}: Props) { const regionVar = api.serverVariables?.region; const regionOptions = useMemo(() => regionVar?.enum ?? [], [regionVar?.enum]); - const userRegion = - codeContext?.codeKeywords.USER && codeContext.codeKeywords.PROJECT[0] - ? detectRegionFromApiUrl(codeContext.codeKeywords.PROJECT[0].API_URL) - : undefined; + const [sharedSelection] = codeContext?.sharedKeywordSelection ?? [ + {} as Record, + ]; + const projectIdx = sharedSelection.PROJECT ?? 0; + const selectedProject = codeContext?.codeKeywords.USER + ? codeContext.codeKeywords.PROJECT[projectIdx] + : undefined; + + const userRegion = selectedProject + ? detectRegionFromApiUrl(selectedProject.API_URL) + : undefined; const [selectedRegion, setSelectedRegion] = useState(regionVar?.default ?? 'us'); @@ -65,9 +88,10 @@ export function ApiExamples({api}: Props) { const resolvedServer = resolveServerUrl(api.server, api.serverVariables, { region: selectedRegion, }); + const resolvedPath = resolveApiPath(api.apiPath, selectedProject); const apiExample = [ - `curl ${resolvedServer}${api.apiPath}`, + `curl ${resolvedServer}${resolvedPath}`, ` -H 'Authorization: Bearer '`, ]; if (['put', 'options', 'delete'].includes(api.method.toLowerCase())) {