NextJs client with an external backend
Unanswered
Brown bear posted this in #help-forum
Brown bearOP
Hi All,
How can I communicate with this external backend? Should I use:
- NextJS server actions.
- Api folder
- Create API client in lib using fetch- I currently have this
How can I communicate with this external backend? Should I use:
- NextJS server actions.
- Api folder
- Create API client in lib using fetch- I currently have this
11 Replies
Which external backend, like what its for.
If its something to perform sensitive actions only performed by a admin, and permissions need to be verified on nextjs app, then 1(for mutations) or 2
If its something to perform sensitive actions only performed by a admin, and permissions need to be verified on nextjs app, then 1(for mutations) or 2
Brown bearOP
this is my lib/api-client.ts. I am not sure if this is the best practise.
"use server";
import { auth } from "@clerk/nextjs/server";
const getBaseUrl = () => {
const baseUrl = process.env.NEXT_PUBLIC_API_URL;
if (!baseUrl) {
throw new Error("NEXT_PUBLIC_API_URL is not defined");
}
return baseUrl;
};
export async function request<T>(
endpoint: string,
options: RequestInit = {}
): Promise<T> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
...((options.headers as Record<string, string>) || {}),
};
const { getToken } = await auth();
const token = await getToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
}
const res = await fetch(`${getBaseUrl()}${endpoint}`, {
...options,
headers,
});
if (!res.ok) {
throw new Error(`Request failed with status ${res.status}`);
}
return res.json();
}
I need to "use server" here to retrieve token from clerk
Backend is .NET C#, All requests will go there.
Brown bearOP
@Anay-208 | Ping in replies
@Brown bear this is my lib/api-client.ts. I am not sure if this is the best practise.
"use server";
import { auth } from "@clerk/nextjs/server";
const getBaseUrl = () => {
const baseUrl = process.env.NEXT_PUBLIC_API_URL;
if (!baseUrl) {
throw new Error("NEXT_PUBLIC_API_URL is not defined");
}
return baseUrl;
};
export async function request<T>(
endpoint: string,
options: RequestInit = {}
): Promise<T> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
...((options.headers as Record<string, string>) || {}),
};
const { getToken } = await auth();
const token = await getToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
}
const res = await fetch(`${getBaseUrl()}${endpoint}`, {
...options,
headers,
});
if (!res.ok) {
throw new Error(`Request failed with status ${res.status}`);
}
return res.json();
}
You'll just get unnecessary costs of server function invocations., its best to get token in frontend, I believe its possible.
Brown bearOP
@Anay-208 | Ping in replies Yeah we are able to get the token in "use client" components aswell. I did not know about the cost for invocations when using "use server"
.
So if my backend is separated, I shouldn't use server in NextJS?

So if my backend is separated, I shouldn't use server in NextJS?
It completely depends, But in this usecase, I wouldn't say you use server for this usecase.
adding "use server" on top of files basically mean that those functions will be executed on the server
adding "use server" on top of files basically mean that those functions will be executed on the server
If your issue is solved, Can you mark solution?