> ## Documentation Index
> Fetch the complete documentation index at: https://docs.base39.com.br/llms.txt
> Use this file to discover all available pages before exploring further.

# Find graph distance between a company and another CPF/CNPJ

> Expande conexões societárias até encontrar o documento alvo e retorna caminhos mínimos e matches de CPF parcial.



## OpenAPI

````yaml /api-reference/openapi.json post /v2/companies/{idOrDocument}/graph/distance
openapi: 3.1.0
info:
  title: Base39 API
  description: Base39 API
  version: 1.0.0
  contact:
    name: Base39 Team
    url: https://base39.com.br
    email: suporte@base39.com.br
  termsOfService: https://base39.com.br/termos
  license:
    name: Proprietary
    url: https://base39.com.br/termos
servers:
  - url: http://localhost:3000
    description: Local
  - url: https://api.dev.base39.cloud
    description: Development
  - url: https://api.base39.com.br
    description: Production
security:
  - OAuth2: []
tags:
  - name: pages
    description: Profiles and reports pages
  - name: pipelines
    description: Pipeline boards (kanban) of pages
  - name: blocks
    description: Blocks operations
  - name: search
    description: Search across content
  - name: targets
    description: Lookup targets via datasets
  - name: CognitoTriggers
    description: Cognito triggers
paths:
  /v2/companies/{idOrDocument}/graph/distance:
    post:
      tags:
        - companies
      summary: Find graph distance between a company and another CPF/CNPJ
      description: >-
        Expande conexões societárias até encontrar o documento alvo e retorna
        caminhos mínimos e matches de CPF parcial.
      operationId: company_find_graph_distance
      parameters:
        - name: idOrDocument
          required: true
          in: path
          description: Company ID (cmp_*) or document (CNPJ)
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CompanyGraphDistanceInput'
      responses:
        '200':
          description: Distance search result
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CompanyGraphDistanceOutput'
        '400':
          description: Invalid target document or input
        '401':
          description: Unauthorized
        '404':
          description: Company not found
components:
  schemas:
    CompanyGraphDistanceInput:
      type: object
      properties:
        document:
          type: string
          description: >-
            Documento alvo para encontrar conexão a partir da empresa base (CPF
            ou CNPJ)
          example: '12345678909'
        ignore:
          description: >-
            Lista de matches de CPF (nome + cpf) que devem ser ignorados na
            busca
          type: array
          items:
            $ref: '#/components/schemas/CompanyGraphDistanceIgnoreInput'
        maxDepth:
          type: number
          description: >-
            Profundidade máxima do grafo para buscar conexões indiretas entre
            empresas e pessoas
          default: 6
          minimum: 1
          maximum: 8
        maxPaths:
          type: number
          description: Quantidade máxima de caminhos de conexão profundos a retornar
          default: 100
          minimum: 1
          maximum: 500
      required:
        - document
    CompanyGraphDistanceOutput:
      type: object
      properties:
        sourceDocument:
          type: string
          description: Documento da empresa de origem (CNPJ)
        targetDocument:
          type: string
          description: Documento alvo normalizado (CPF ou CNPJ)
        targetType:
          type: string
          enum:
            - cpf
            - cnpj
        found:
          type: boolean
          description: Indica se a conexão foi encontrada
        distance:
          type: object
          description: Distância mínima em arestas, quando encontrada
        expansions:
          type: number
          description: Quantidade total de expansões realizadas no dataset
        paths:
          description: >-
            Todos os caminhos mínimos encontrados (array de IDs de nós por
            caminho)
          items:
            type: array
          type: array
        deepPaths:
          description: >-
            Caminhos profundos encontrados entre origem e destino (empresas e
            pessoas), respeitando maxDepth
          items:
            type: array
          type: array
        cpfMatches:
          description: Matches de CPF parcial encontrados (incluindo ignorados)
          type: array
          items:
            $ref: '#/components/schemas/CompanyGraphCpfMatchOutput'
        companiesBetween:
          description: >-
            Empresas intermediárias encontradas entre origem e destino (exclui
            os pontos de origem/destino)
          type: array
          items:
            $ref: '#/components/schemas/CompanyGraphDistanceNodeOutput'
        peopleBetween:
          description: >-
            Pessoas intermediárias encontradas entre origem e destino (exclui os
            pontos de origem/destino)
          type: array
          items:
            $ref: '#/components/schemas/CompanyGraphDistanceNodeOutput'
        entitiesBetween:
          description: Todas as entidades intermediárias encontradas (empresas e pessoas)
          type: array
          items:
            $ref: '#/components/schemas/CompanyGraphDistanceNodeOutput'
        nodes:
          type: array
          items:
            $ref: '#/components/schemas/CompanyGraphDistanceNodeOutput'
        edges:
          type: array
          items:
            $ref: '#/components/schemas/CompanyGraphDistanceEdgeOutput'
      required:
        - sourceDocument
        - targetDocument
        - targetType
        - found
        - expansions
        - paths
        - deepPaths
        - cpfMatches
        - companiesBetween
        - peopleBetween
        - entitiesBetween
        - nodes
        - edges
    CompanyGraphDistanceIgnoreInput:
      type: object
      properties:
        name:
          type: string
          description: Nome do sócio a ignorar em caso de match parcial de CPF
          example: JOAO DA SILVA
        cpf:
          type: string
          description: CPF completo (11 dígitos) usado para ignorar o match
          example: '12345678909'
      required:
        - name
        - cpf
    CompanyGraphCpfMatchOutput:
      type: object
      properties:
        nodeId:
          type: string
          description: ID do nó de pessoa encontrado
        name:
          type: string
          description: Nome do sócio encontrado no grafo
        partialCpf:
          type: string
          description: CPF parcial retornado pelo BigQuery
        ignored:
          type: boolean
          description: Indica se o match foi ignorado pelo usuário
      required:
        - nodeId
        - name
        - partialCpf
        - ignored
    CompanyGraphDistanceNodeOutput:
      type: object
      properties:
        id:
          type: string
          description: ID único do nó no grafo
        type:
          type: string
          description: Tipo lógico do nó retornado pelo dataset
        label:
          type: string
          description: Rótulo visível do nó
        entityType:
          type: string
          enum:
            - company
            - person
        metadata:
          $ref: '#/components/schemas/CompanyGraphNodeMetadataOutput'
      required:
        - id
        - type
        - label
        - entityType
        - metadata
    CompanyGraphDistanceEdgeOutput:
      type: object
      properties:
        source:
          type: string
        target:
          type: string
        type:
          type: string
        label:
          type: string
      required:
        - source
        - target
        - type
        - label
    CompanyGraphNodeMetadataOutput:
      type: object
      properties:
        cnpjBase:
          type: string
          description: CNPJ base (quando aplicável)
        tradeName:
          type: string
          description: Nome fantasia (quando aplicável)
        document:
          type: string
          description: Documento do nó (CNPJ completo ou CPF parcial)
        qualification:
          type: string
          description: Qualificação societária
        startDate:
          type: string
          description: Data de entrada na sociedade
        role:
          type: string
          description: Papel do sócio no dataset de origem
  securitySchemes:
    OAuth2:
      type: oauth2
      flows:
        authorizationCode:
          authorizationUrl: https://id.local.base39.cloud/oauth2/authorize
          tokenUrl: https://id.local.base39.cloud/oauth2/token
          scopes:
            profile: Read user profile
            email: Read user email
            openid: OpenID authentication
      description: OAuth2 Authorization Code flow

````