freelancer-kit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MURTESA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,28 @@
1
+ const { SearchProjects } = require("../src/index");
2
+
3
+ const projects = new SearchProjects({
4
+ accessToken: "access_token",
5
+ sandbox: true,
6
+ });
7
+
8
+ (async () => {
9
+ try {
10
+ const result = await projects.search({
11
+ query: "software",
12
+ project_types: ["fixed"],
13
+ min_price: 50,
14
+ max_price: 1000,
15
+ jobs: [1, 2, 3], // job id
16
+ languages: ["en"],
17
+ project_statuses: ["active"],
18
+ sort_field: "time_updated",
19
+ limit: 10,
20
+ full_description: true,
21
+ user_details: true,
22
+ });
23
+
24
+ console.log(result.result.projects);
25
+ } catch (err) {
26
+ console.error("Error:", err.details || err.message);
27
+ }
28
+ })();
@@ -0,0 +1,31 @@
1
+ const { FreelancerAuth } = require("../src/index");
2
+
3
+ (async () => {
4
+ const clientId = "app_id";
5
+ const clientSecret = "client_secret";
6
+ const redirectUri = "https://example.com/callback";
7
+ const sandbox = true;
8
+
9
+ const flags = {
10
+ messaging: true,
11
+ project_create: true,
12
+ project_manage: false,
13
+ contest_create: false,
14
+ contest_manage: false,
15
+ user_information: true,
16
+ location_tracking_create: false,
17
+ location_tracking_view: false,
18
+ };
19
+
20
+ const auth = new FreelancerAuth({ clientId, clientSecret, redirectUri, sandbox, flags });
21
+
22
+ const authUrl = auth.generateAuthLink();
23
+ console.log("Open this link in your browser to authorize the app:");
24
+ console.log(authUrl);
25
+ const code = await FreelancerAuth.askCode("Enter the code you received: ");
26
+
27
+ const tokens = await auth.exchangeCode(code.trim());
28
+
29
+ console.log("Access Token:", tokens.access_token);
30
+ console.log("Refresh Token:", tokens.refresh_token);
31
+ })();
@@ -0,0 +1,15 @@
1
+ const { TOKEN } = require("../src/index");
2
+
3
+ const client = new TOKEN({
4
+ token: "access_token",
5
+ sandbox: true,
6
+ });
7
+
8
+ (async () => {
9
+ try {
10
+ const valid = await client.validateToken();
11
+ console.log("Token valid?", valid);
12
+ } catch (err) {
13
+ console.error("Error validating token:", err.message);
14
+ }
15
+ })();
@@ -0,0 +1,20 @@
1
+ const { FreelancerAuth } = require("../src");
2
+
3
+ (async () => {
4
+ try {
5
+ const auth = new FreelancerAuth({
6
+ clientId: "app_id",
7
+ clientSecret: "client_secret",
8
+ sandbox: true,
9
+ });
10
+
11
+ auth.refreshToken = "refresh_token";
12
+
13
+ const tokens = await auth.refreshTokenRequest();
14
+
15
+ console.log("New Access Token:", tokens.access_token);
16
+ console.log("New Refresh Token:", tokens.refresh_token);
17
+ } catch (err) {
18
+ console.error("Failed to refresh token:", err.message);
19
+ }
20
+ })();
@@ -0,0 +1,19 @@
1
+ const { SelfProfile } = require("../src/index");
2
+ (async () => {
3
+ try {
4
+ const profile = new SelfProfile({
5
+ accessToken: "access_token",
6
+ sandbox: true
7
+ });
8
+
9
+ const res = await profile.getMyProfile({
10
+ avatar: true,
11
+ display_info: true,
12
+ profile_description: true,
13
+ });
14
+
15
+ console.log(res);
16
+ } catch (err) {
17
+ console.error("Error:", err.message);
18
+ }
19
+ })();
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "freelancer-kit",
3
+ "version": "1.0.0",
4
+ "description": "Unofficial Node.js toolkit for integrating with the Freelancer.com API",
5
+ "author": "MURTESA",
6
+ "license": "MIT",
7
+ "main": "src/index.js",
8
+ "keywords": [
9
+ "freelancer",
10
+ "freelancer-kit",
11
+ "freelancer-api",
12
+ "nodejs",
13
+ "sdk",
14
+ "client",
15
+ "unofficial"
16
+ ],
17
+ "engines": {
18
+ "node": ">=14"
19
+ },
20
+ "dependencies": {
21
+ "node-fetch": "^2.7.0"
22
+ }
23
+ }
@@ -0,0 +1,111 @@
1
+ const fetch = require("node-fetch");
2
+ const readline = require("readline");
3
+
4
+ class FreelancerAuth {
5
+ constructor({ clientId, clientSecret, redirectUri = "http://localhost", sandbox = false, flags = {} }) {
6
+ if (!clientId || !clientSecret) throw new Error("clientId and clientSecret are required");
7
+
8
+ this.clientId = clientId;
9
+ this.clientSecret = clientSecret;
10
+ this.redirectUri = redirectUri;
11
+ this.sandbox = sandbox;
12
+ this.flags = flags;
13
+ this.token = null;
14
+ this.refreshToken = null;
15
+
16
+ this.oauthUrl = sandbox
17
+ ? "https://accounts.freelancer-sandbox.com/oauth"
18
+ : "https://accounts.freelancer.com/oauth";
19
+ }
20
+
21
+ generateAuthLink() {
22
+ const scopes = ["basic"];
23
+ if (this.flags.project_create) scopes.push("1");
24
+ if (this.flags.project_manage) scopes.push("2");
25
+ if (this.flags.contest_create) scopes.push("3");
26
+ if (this.flags.contest_manage) scopes.push("4");
27
+ if (this.flags.messaging) scopes.push("5");
28
+ if (this.flags.user_information) scopes.push("6");
29
+ if (this.flags.location_tracking_create) scopes.push("7");
30
+ if (this.flags.location_tracking_view) scopes.push("8");
31
+
32
+ const scopeString = scopes.join(" ");
33
+ return `${this.oauthUrl}/authorise?response_type=code&client_id=${this.clientId}&redirect_uri=${encodeURIComponent(
34
+ this.redirectUri
35
+ )}&scope=${encodeURIComponent(scopeString)}`;
36
+ }
37
+
38
+ async exchangeCode(code) {
39
+ const url = `${this.oauthUrl}/token`;
40
+ const params = new URLSearchParams({
41
+ grant_type: "authorization_code",
42
+ client_id: this.clientId,
43
+ client_secret: this.clientSecret,
44
+ code: code.trim(),
45
+ redirect_uri: this.redirectUri,
46
+ });
47
+
48
+ const res = await fetch(url, {
49
+ method: "POST",
50
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
51
+ body: params.toString(),
52
+ });
53
+
54
+ const text = await res.text();
55
+ if (!res.ok) throw new Error(`Token request failed: ${res.status} ${res.statusText}\n${text}`);
56
+
57
+ const data = JSON.parse(text);
58
+ this.token = data.access_token;
59
+ this.refreshToken = data.refresh_token;
60
+ return data;
61
+ }
62
+
63
+ async refreshTokenRequest() {
64
+ if (!this.refreshToken) throw new Error("No refresh token available");
65
+
66
+ const url = `${this.oauthUrl}/token`;
67
+ const params = new URLSearchParams({
68
+ grant_type: "refresh_token",
69
+ client_id: this.clientId,
70
+ client_secret: this.clientSecret,
71
+ refresh_token: this.refreshToken,
72
+ });
73
+
74
+ const res = await fetch(url, {
75
+ method: "POST",
76
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
77
+ body: params.toString(),
78
+ });
79
+
80
+ const text = await res.text();
81
+ if (!res.ok) throw new Error(`Refresh token request failed: ${res.status} ${res.statusText}\n${text}`);
82
+
83
+ const data = JSON.parse(text);
84
+ this.token = data.access_token;
85
+ this.refreshToken = data.refresh_token;
86
+ return data;
87
+ }
88
+
89
+ async request(endpoint, options = {}) {
90
+ if (!this.token) throw new Error("No access token, authenticate first");
91
+ const url = `https://www.freelancer.com/api${endpoint}`;
92
+ const res = await fetch(url, {
93
+ ...options,
94
+ headers: {
95
+ Authorization: "Bearer " + this.token,
96
+ Accept: "application/json",
97
+ "Content-Type": "application/json",
98
+ ...(options.headers || {}),
99
+ },
100
+ });
101
+ const text = await res.text();
102
+ if (!res.ok) throw new Error(`API request failed: ${res.status} ${res.statusText}\n${text}`);
103
+ return JSON.parse(text);
104
+ }
105
+
106
+ static askCode(promptText = "Enter the code you received: ") {
107
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
108
+ return new Promise((resolve) => rl.question(promptText, (ans) => { rl.close(); resolve(ans); }));
109
+ }
110
+ }
111
+ module.exports = FreelancerAuth;
@@ -0,0 +1,52 @@
1
+ const fetch = require("node-fetch");
2
+
3
+ class SearchProjects {
4
+ constructor({ accessToken, sandbox = false }) {
5
+ if (!accessToken) {
6
+ throw new Error("accessToken is required");
7
+ }
8
+
9
+ this.accessToken = accessToken;
10
+ this.baseUrl = sandbox
11
+ ? "https://www.freelancer-sandbox.com/api/projects/0.1"
12
+ : "https://www.freelancer.com/api/projects/0.1";
13
+ }
14
+
15
+
16
+ async search(filters = {}) {
17
+ const url = new URL(`${this.baseUrl}/projects/all/`);
18
+
19
+ Object.entries(filters).forEach(([key, value]) => {
20
+ if (value === undefined || value === null) return;
21
+
22
+ if (Array.isArray(value)) {
23
+ value.forEach((v) => {
24
+ url.searchParams.append(`${key}[]`, v);
25
+ });
26
+ } else {
27
+ url.searchParams.append(key, value);
28
+ }
29
+ });
30
+
31
+ const res = await fetch(url.toString(), {
32
+ method: "GET",
33
+ headers: {
34
+ "Content-Type": "application/json",
35
+ "freelancer-oauth-v1": this.accessToken,
36
+ },
37
+ });
38
+
39
+ const data = await res.json();
40
+
41
+ if (!res.ok) {
42
+ const error = new Error(data.message || "Freelancer API Error");
43
+ error.status = res.status;
44
+ error.details = data;
45
+ throw error;
46
+ }
47
+
48
+ return data;
49
+ }
50
+ }
51
+
52
+ module.exports = SearchProjects;
@@ -0,0 +1,89 @@
1
+ const fetch = require("node-fetch");
2
+
3
+ class SelfProfile {
4
+ constructor({ accessToken, sandbox = false }) {
5
+ if (!accessToken) {
6
+ throw new Error("accessToken is required");
7
+ }
8
+
9
+ this.accessToken = accessToken;
10
+ this.sandbox = sandbox;
11
+
12
+ this.baseUrl = sandbox
13
+ ? "https://www.freelancer-sandbox.com/api/users/0.1"
14
+ : "https://www.freelancer.com/api/users/0.1";
15
+ }
16
+
17
+ buildQuery(params = {}) {
18
+ const query = new URLSearchParams();
19
+
20
+ for (const [key, value] of Object.entries(params)) {
21
+ if (value === true) {
22
+ query.append(key, "true");
23
+ } else if (value !== false && value != null) {
24
+ query.append(key, String(value));
25
+ }
26
+ }
27
+
28
+ return query.toString();
29
+ }
30
+
31
+ async request(endpoint) {
32
+ const res = await fetch(this.baseUrl + endpoint, {
33
+ headers: {
34
+ Accept: "application/json",
35
+ "freelancer-oauth-v1": this.accessToken,
36
+ },
37
+ });
38
+
39
+ const data = await res.json();
40
+ if (!res.ok) {
41
+ throw new Error(JSON.stringify(data, null, 2));
42
+ }
43
+
44
+ return data;
45
+ }
46
+
47
+ filterProfile(user, params) {
48
+ const result = {};
49
+
50
+ if (params.avatar === true) {
51
+ result.avatar = user.avatar_xlarge_cdn || user.avatar_xlarge;
52
+ }
53
+
54
+ if (params.display_info === true) {
55
+ result.display_name = user.display_name;
56
+ result.tagline = user.tagline;
57
+ result.location = user.location;
58
+ }
59
+
60
+ if (params.profile_description === true) {
61
+ result.profile_description = user.profile_description;
62
+ }
63
+
64
+ if (params.reputation === true) {
65
+ result.reputation = user.reputation;
66
+ }
67
+
68
+ if (params.cover_image === true) {
69
+ result.cover_image = user.cover_image;
70
+ }
71
+
72
+ return result;
73
+ }
74
+
75
+ async getMyProfile(params = {}) {
76
+ const query = this.buildQuery(params);
77
+ const endpoint = `/self/${query ? `?${query}` : ""}`;
78
+
79
+ const response = await this.request(endpoint);
80
+
81
+ if (!response || !response.result) {
82
+ throw new Error("Invalid API response");
83
+ }
84
+
85
+ return this.filterProfile(response.result, params);
86
+ }
87
+ }
88
+
89
+ module.exports = SelfProfile;
@@ -0,0 +1,40 @@
1
+ const fetch = require("node-fetch");
2
+
3
+ class Token {
4
+ constructor({ token, sandbox = false }) {
5
+ if (!token) throw new Error("Token is required");
6
+ this.token = token;
7
+ this.baseUrl = sandbox
8
+ ? "https://www.freelancer-sandbox.com/api"
9
+ : "https://www.freelancer.com/api";
10
+ }
11
+
12
+ async request(endpoint, options = {}) {
13
+ const url = this.baseUrl + endpoint;
14
+ const res = await fetch(url, {
15
+ ...options,
16
+ headers: {
17
+ Authorization: "Bearer " + this.token,
18
+ Accept: "application/json",
19
+ "Content-Type": "application/json",
20
+ ...(options.headers || {}),
21
+ },
22
+ });
23
+ if (!res.ok) {
24
+ const text = await res.text();
25
+ throw new Error(`Freelancer API error: ${res.status} ${res.statusText}\n${text}`);
26
+ }
27
+ return res.json();
28
+ }
29
+
30
+ async validateToken() {
31
+ try {
32
+ await this.request("/users/0.1/self/");
33
+ return true;
34
+ } catch {
35
+ return false;
36
+ }
37
+ }
38
+ }
39
+
40
+ module.exports = Token;
package/src/index.js ADDED
@@ -0,0 +1,14 @@
1
+ const SelfProfile = require("./classes/SelfProfile");
2
+ const Token = require("./classes/Token");
3
+ const SearchProjects = require("./classes/SearchProjects");
4
+ const FreelancerAuth = require("./classes/FreelancerAuth");
5
+ const Bid = require("./classes/Bid")
6
+
7
+
8
+ module.exports = {
9
+ SelfProfile,
10
+ Token,
11
+ SearchProjects,
12
+ FreelancerAuth,
13
+ Bid,
14
+ };