Function Documentation
  • Developer Platform
    • Overview
    • Service Descriptions
    • First Party Library Support
      • Python Example
      • Typescript & Node Backend Example
    • OpenAI Compatible API
  • Supported Models
    • Chat & Code Completion
    • Embeddings
    • Text To Image
    • Transcription
  • Consumer Demos
    • Function Chat
Powered by GitBook
On this page
  • Step 1: Add the required registry
  • Step 2: Add required dependencies
  • Step 3: Create the API Client
  • Step 4: Call our API services
  1. Developer Platform
  2. First Party Library Support

Typescript & Node Backend Example

PreviousPython ExampleNextOpenAI Compatible API

Last updated 6 months ago

See our full reference implementation .

Step 1: Add the required registry

npm config set @buf:registry https://buf.build/gen/npm/v1/

Step 2: Add required dependencies

npm i @connectrpc/connect
npm i @connectrpc/connect-node
npm i @buf/fxnlabs_api-gateway.connectrpc_es

Step 3: Create the API Client

import {createPromiseClient, Interceptor} from "@connectrpc/connect";
import { createConnectTransport } from "@connectrpc/connect-node";
import {
    APIGatewayService
} from "@buf/fxnlabs_api-gateway.connectrpc_es/apigateway/v1/apigateway_connect.js";

function createAPIClient() {
    // Adds API Key to each request
    const apiKeyInterceptor: Interceptor = (next) => async (req) => {
        req.header.set("x-api-key", process.env.FXN_API_KEY!)
        return await next(req);
    };

    // Creates the transport
    const transport = createConnectTransport({
        httpVersion: "1.1",
        baseUrl: "api.function.network",
        interceptors: [apiKeyInterceptor]
    });

    // Creates the client
    return createPromiseClient(APIGatewayService, transport);
}

Step 4: Call our API services

// Init the API Client
const apiClient = createAPIClient()

// Use API Client
const response = await apiClient.embed({
    model: "bge-small-en-v1.5",
    input: "Embed me so I can use it for RAG Pipelines",
})

// Log responses
console.log(response.object)
console.log(response.data)
here