🖊️How to write API logics

When creating APIs on Blockflow, you define the logic that handles incoming requests, interacts with databases, and generates appropriate responses using TypeScript. Before you start coding, it's essential to understand the following concepts:

  1. Context

  2. Database Interactions

  3. Secrets

  4. Available Libraries

Context

The context variable is injected into the API handler function and provides access to the incoming request and the outgoing response.

  1. Request: The request object within the context allows you to access query parameters passed when calling the API. For example:

This variable gives your access to query parameters you passed while calling APIs.

const user = context.request.query.user;

This code retrieves the value of the user query parameter from the API request.

  1. Response: The response object within the context is used to generate the API response that will be returned to the client. You can assign values to the response object and return it at the end of the handler function.

For example:

export const handler = async (context: any, bind: Function) => {
    let {
        request,
        response
    } = context;
    
    response = { 
        x: "1"
    };

    return response;
};

Your API response will look like:-

{
    x: '1'
}

Database Interactions

Blockflow provides a seamless way to interact with your managed databases within your API logic. To learn how to read from and write to your databases, refer to this guide.

Secrets

Secrets allow you to securely store and access sensitive information, such as API keys or private keys, without hardcoding them in your API logic. You can access secrets using the secret variable.

For example:

// accessing a secret
const wallet = new ethers.Wallet(secret.Private_Key);

To manage your secrets, use Blockflow's Secret Manager. Refer to this guide for more information.

Libraries

Blockflow provides several pre-imported libraries that you can use in your API logic:

  1. Ethers.js: A popular Ethereum library for interacting with the Ethereum blockchain and smart contracts.

You can check out the library here.

Example usage:

const wallet = new ethers.Wallet(secret.Private_Key);
  1. BigNumber.js: A library for handling large numbers and performing arithmetic operations.

You can check out the library here.

Example usage:

let userAmount = new BigNumber(amount);
  1. PushUtils: A utility library provided by Blockflow for interacting with the Push API.

You can check out the library here.

Example usage:

const user = await PushUtils.PushAPI.initialize(
    wallet, 
    {
        env: PushUtils.CONSTANTS.ENV.PROD
    }
);

Writing Your API Logic

Now that you understand the key concepts, you can start writing your API logic. Here's a basic template to get you started:

import { IContext } from "@blockflow-api/utils";

export const handler = async (context: IContext, bind: Function) => {
  const { request, response } = context;

  // Access request parameters
  const userId = request.query.userId;

  // Interact with databases
  const userDB = bind("Users");
  const user = await userDB.findOne({ id: userId });

  // Generate the response
  response = {
    user: user,
  };

  return response;
};

In the handler function, you can access the request and response objects from the context, interact with databases using the bind function, and generate the appropriate response.

Remember to handle errors appropriately and return meaningful responses to the client.

Conclusion

Writing API logic on Blockflow using TypeScript provides a powerful and flexible way to define the behavior of your APIs. By leveraging the context object, database interactions, secrets, and available libraries, you can create robust and efficient APIs that meet your application's requirements.

If you have any further questions or need assistance with writing API logic on Blockflow, please reach out to our support team via Discord.

Happy building with Blockflow APIs!