- The API: https://coders-network-api.onrender.com/
- The code: https://github.com/TechmongersNL/coders-network-api
The Techmongers Coders Network API is just a simple REST API. Sadly, it does not have a UI yet. Will you help us make it? :)
We've documented all the available endpoints below. Each endpoint has two examples, one that you can use with HTTPie in your terminal, and one that you can use directly in your DevTools. We encourage you to do this!
We recommend you use axios
to make your requests. Some request examples are:
// inside an async function
try {
// Simple GET request
const response = await axios.get(
"https://coders-network-api.onrender.com/hello"
);
console.log(response.data);
} catch (error) {
console.log(error.message);
}
// All these should also be wrapped in async functions and try/catch blocks:
// POST request
const response = await axios.post(
"https://coders-network-api.onrender.com/login",
{ email: "[email protected]", password: "somepassword" } // these are the body parameters
);
// Authorized post request
const response = await axios.post(
"https://coders-network-api.onrender.com/posts",
{ title: "My new post", content: "lorem ipsum" },
{
headers: {
Authorization: "Bearer <yourJWTtoken>", // Setting the JWT header
},
}
);
If we want to avoid having to repeat the first part of the URL many times (what is sometimes called the baseURL
) we can create a preconfigured axios instance. To do this you can create an axios.js
file, maybe inside the /store
folder in your frontend. In this file you can do the following:
// /src/store/axios.js
import axios from "axios";
export default axios.create({
baseURL: "https://coders-network-api.onrender.com",
});
Then from another file (like the actions files):
import axios from "../axios"; // Import axios from our file, not the library
export const fetchPosts = () => async (dispatch, getState) => {
try {
const response = await axios.get("/posts");
console.log(response.data);
} catch (e) {
console.log(e.message);
}
};
The simplest endpoint of all, just to see if everything's still working.
-
HTTPie:
http -v GET https://coders-network-api.onrender.com/hello
-
JavaScript:
axios .get("https://coders-network-api.onrender.com/hello") .then((response) => console.log("data", response.data)) .catch((err) => console.log("err", err));
-
Response:
{ message: "Hello world!"; }
The API knows of the following entity types:
- Developers
- Technologies
- Posts
- Tags
- Comments
- Likes
To give you a feeling of what these objects look like, here are some TypeScript definitions for them:
interface Developer_Meta {
id: number;
name: string;
email: string;
}
interface Developer extends Developer_Meta {
github_username: null | string;
website: null | string;
posts: Post_Meta[];
technologies: Technology[];
createdAt: string;
}
interface Technology {
id: number;
title: string;
}
interface Post_Meta {
id: number;
title: string;
tags: Tag[];
post_likes: Like[];
createdAt: string;
updatedAt: string;
}
interface Post extends Post_Meta {
content: string;
}
interface Like {
developer: Developer_Meta;
createdAt: string;
}
interface Comment {
id: number;
text: string;
developer: Developer_Meta;
createdAt: string;
updatedAt: string;
}
We use JSON Web Tokens for authentication. Some endpoints are authenticated, which means you can only get data from them if you're logged in.
- To login, you want to
POST
to/login
with your email and password, and then you get a JWT back. - To access an authenticated endpoint after you've logged in, you have to send the JWT along as a header. We have a simple test authenticated endpoint at
/me
. - To start off, for testing purposes only, you can use the "fake" (and misformed) JWT
faketokenforkelley238765293
, which will always let you login as user #1.
-
HTTPie:
http -v POST https://coders-network-api.onrender.com/signup name="Kelley van Evert" [email protected] password=abcd
-
JavaScript:
axios .post("/signup", { name: "Kelley van Evert", email: "[email protected]", password: "abcd", }) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ jwt: string; }
-
HTTPie:
http -v POST https://coders-network-api.onrender.com/login [email protected] password=abcd
-
JavaScript:
axios .post("/login", { email: "[email protected]", password: "abcd", }) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ jwt: string; }
-
HTTPie:
http -v GET https://coders-network-api.onrender.com/me Authorization:"Bearer JWT"
-
JavaScript:
axios .get("/me", { headers: { Authorization: "Bearer <yourJWTtoken>" } }) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
Developer
-
HTTPie:
http -v GET https://coders-network-api.onrender.com/posts/1
-
JavaScript:
axios .get(`/posts/1`) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
Post
This is an authenticated API endpoint.
-
HTTPie:
http -v POST https://coders-network-api.onrender.com/posts/1/likes Authorization:"Bearer JWT"
-
JavaScript:
axios .post( `/posts/1/likes`, {}, // empty body object, no body parameters are sent. { headers: { Authorization: "Bearer <yourJWTtoken>" } } ) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ ok: true; }
This is an authenticated API endpoint.
-
HTTPie:
http -v DELETE https://coders-network-api.onrender.com/posts/1/likes Authorization:"Bearer JWT"
-
JavaScript:
axios .delete(`/posts/1/likes`, { headers: { Authorization: "Bearer <yourJWTtoken>" }, }) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ ok: true; }
-
HTTPie:
http -v GET https://coders-network-api.onrender.com/posts/1/comments
-
JavaScript:
axios .get(`/posts/1/comments`) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ count: number; rows: Comment[]; }
This is an authenticated API endpoint. The new comment is made in the name of the user currently logged in.
-
HTTPie:
http -v POST https://coders-network-api.onrender.com/posts/1/comments Authorization:"Bearer JWT" text="Love it!"
-
JavaScript:
axios .post( `/posts/1/comments`, { text: "Love it!", }, { headers: { Authorization: "Bearer <yourJWTtoken>", }, } ) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
Comment
Paginated with the optional offset
and limit
query parameters.
-
HTTPie:
http -v GET "https://coders-network-api.onrender.com/posts?offset=1&limit=2"
-
JavaScript:
axios .get(`/posts?offset=1&limit=2`) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ count: number; rows: Post[]; }
Paginated with the optional offset
and limit
query parameters.
-
HTTPie:
http -v GET "https://coders-network-api.onrender.com/posts?tag=github"
-
JavaScript:
axios .get(`/posts?tag=github`) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ count: number; rows: Post[]; }
Paginated with the optional offset
and limit
query parameters.
-
HTTPie:
http -v GET "https://coders-network-api.onrender.com/posts?author=1"
-
JavaScript:
axios .get(`/posts?author=2`) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ count: number; rows: Post[]; }
This is an authenticated API endpoint. The new post is made in the name of the user currently logged in.
-
HTTPie:
http -v POST https://coders-network-api.onrender.com/posts Authorization:"Bearer JWT" title="ABC" content="bla bla bla"
-
JavaScript:
axios .post( "/posts", { title: "ABC", content: "bla bla bla", }, { headers: { Authorization: "Bearer <yourJWTtoken>" }, } ) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
Post
This is an authenticated API endpoint. The new post is made in the name of the user currently logged in.
You don't have to send all post fields. Only the included fields will be updated.
-
HTTPie:
http -v PUT https://coders-network-api.onrender.com/posts Authorization:"Bearer JWT" title="DEF"
-
JavaScript:
axios .put( "/posts/1", { title: "DEF", }, { headers: { Authorization: "Bearer <yourJWTtoken>" }, } ) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
Post
This is an authenticated API endpoint. The post must be owned by the user currently logged in.
-
HTTPie:
http -v DELETE https://coders-network-api.onrender.com/posts/1 Authorization:"Bearer JWT"
-
JavaScript:
axios .delete("/posts/1", { headers: { Authorization: "Bearer <yourJWTtoken>" }, }) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ id: number; }
Refer to the /signup
endpoint above.
-
HTTPie:
http -v GET https://coders-network-api.onrender.com/developers/1
-
JavaScript:
axios .get(`/developers/1`) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
Developer
Paginated with the optional offset
and limit
query parameters.
-
HTTPie:
http -v GET "https://coders-network-api.onrender.com/developers?offset=1&limit=2"
-
JavaScript:
axios .get(`/developers?offset=1&limit=2`) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ count: number; rows: Developer[]; }
This is an authenticated API endpoint. You can of course only edit your own profile.
-
HTTPie:
http -v PUT https://coders-network-api.onrender.com/developers/1 Authorization:"Bearer JWT" name="Bla" github_username="blabla"
-
JavaScript:
axios .put( `/developers/1`, { name: "Bla", github_username: "blabla", }, { headers: { Authorization: "Bearer <yourJWTtoken>" }, } ) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
Developer
This is an authenticated API endpoint. You can of course only delete your own account.
-
HTTPie:
http -v DELETE https://coders-network-api.onrender.com/developers/1 Authorization:"Bearer JWT"
-
JavaScript:
axios .delete(`/developers/1`, { headers: { Authorization: "Bearer <yourJWTtoken>" }, }) .then((data) => console.log("data", data)) .catch((err) => console.log("err", err));
-
Response:
{ id: number; }