First request
TIP
For more information about using Connect RPC protocol, please refer to Connect RPC docs.
Let's start with the first request. We will go step by step.
Initializing the client
Import the following modules:
import { createClient } from '@connectrpc/connect'
import { createConnectTransport } from '@connectrpc/connect-node'Then initialize the client with the following code:
const transport = createConnectTransport({
httpVersion: "1.1",
baseUrl: "https://api.ibuy.exchange",
});Creating middleware to provide out authorization token
WARNING
Never store your authorization token somewhere else outside your env variables.
Change code of your client initialization to the following:
import { createClient } from '@connectrpc/connect'
import { createClient, type Interceptor } from '@connectrpc/connect'
import { createConnectTransport } from '@connectrpc/connect-node'
const token = "Bearer <my-token>"
const authorizer: Interceptor = (next) => async (req) => {
req.header.set("Authorization", token)
return await next(req);
};
const transport = createConnectTransport({
httpVersion: "1.1",
baseUrl: "https://demo.connectrpc.com",
interceptors: [
authorizer
]
});Initializing the service
We are using Connect RPC protocol which leads us to using separated services for different bussines logic.
Add the following import:
import * as ibuy from '@ibuy.exchange/api'From now, you can access all the services from the ibuy namespace.
Let's fetch our balance. Add the following code:
const marketService = createClient(ibuy.marketv1.PubMarketService, transport)In here you can see that we have provided our Market service in the createClient function.
Check more about supported services and methods in the methods section
Making a request
Now we can make a request to the Market service.
const response = await marketService.fetchBalance({})
console.info(response.balance)Conclusion
For now your whole code should look like this:
import { createClient, type Interceptor } from '@connectrpc/connect'
import { createConnectTransport } from '@connectrpc/connect-node'
import * as ibuy from '@ibuy.exchange/api'
const token = "Bearer <my-token>"
const authorizer: Interceptor = (next) => async (req) => {
req.header.set("Authorization", token)
return await next(req);
};
const transport = createConnectTransport({
httpVersion: "1.1",
baseUrl: "https://api.ibuy.exchange",
interceptors: [
authorizer
]
});
const marketService = createClient(ibuy.marketv1.PubMarketService, transport)
const response = await marketService.fetchBalance({})
console.info(response.balance)Congratulations! You have successfully made your first request to the IBuy API. Next, you can check other supported methods and services.
