Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion cli/src/connect/figma_rest_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,22 @@ export function getApiUrl(figmaNode: string, apiUrlOverride?: string) {
return 'https://api.figma.com/v1'
}

/**
* Returns the appropriate headers for Figma API requests.
*
* Figma supports two authentication methods with different headers:
* - Personal Access Tokens (PAT): `X-Figma-Token` header
* - OAuth2 access tokens: `Authorization: Bearer` header
*
* OAuth2 tokens are detected by their `figu_` prefix.
*
* @see https://developers.figma.com/docs/rest-api/authentication/
*/
export function getHeaders(accessToken: string) {
return {
'X-Figma-Token': accessToken,
...(accessToken.startsWith('figu_')
? { Authorization: `Bearer ${accessToken}` }
: { 'X-Figma-Token': accessToken }),
'Content-Type': 'application/json',
'User-Agent': `code-connect-cli/${version}`,
}
Expand Down
7 changes: 2 additions & 5 deletions cli/src/connect/wizard/run_wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BaseCommand, getAccessToken, getCodeConnectObjects, getDir } from '../.
import prompts from 'prompts'
import fs from 'fs'
import { exitWithFeedbackMessage, findComponentsInDocument, parseFileKey } from '../helpers'
import { FigmaRestApi, getApiUrl } from '../figma_rest_api'
import { FigmaRestApi, getApiUrl, getHeaders } from '../figma_rest_api'
import { exitWithError, logger, success } from '../../common/logging'
import {
ReactProjectInfo,
Expand Down Expand Up @@ -105,10 +105,7 @@ async function fetchTopLevelComponentsFromFile({
) as CliDataResponse,
})
: request.get<CliDataResponse>(apiUrl, {
headers: {
'X-Figma-Token': accessToken,
'Content-Type': 'application/json',
},
headers: getHeaders(accessToken),
})
).finally(() => {
if (cmd.verbose) {
Expand Down