Skip to main content

API Requests

Here is an example of a user creating Plexus API Instances to communicate with a server.

/pages/index.tsx
import { api } from '@plexusjs/core'
// this baseURL should lead to a path on your server. So We can point at `api/v1` for example.
const apiv1 = api('/api/v1')
interface User {
name: string
age: number
}
export default function Home() {
const getUsers = async () => {
// Here you chose a method to call the API, also providing a path and a payload (if needed).
const response = await apiv1.get<User[]>('/users')

if (response.ok) {
// response.data will be of type User[]
console.log(response.data)
}
}
return (
<div>
{data.map((v) => (
<button onClick={() => getUsers()}>Call Api</button>
))}
</div>
)
}