New: GET /api/key

This commit is contained in:
synt-xerror
2026-02-23 08:31:26 -03:00
parent 0543a9e9ce
commit b9b474af40
4 changed files with 41 additions and 70 deletions

View File

@@ -1,12 +1,6 @@
# Neocities C API # Neocities API C library
Minimal, secure C wrapper for the Neocities API using API key authentication. Minimal, secure C wrapper for the Neocities API.
No login flow.
No credential storage.
Stateless by design.
Users must generate their own API key via the Neocities dashboard.
--- ---
@@ -16,14 +10,9 @@ Users must generate their own API key via the Neocities dashboard.
* File listing * File listing
* Upload files * Upload files
* Delete files * Delete files
* Get API key
* TLS verification enabled by default * TLS verification enabled by default
Authentication is performed exclusively via:
```
Authorization: Bearer <API_KEY>
```
--- ---
## Requirements ## Requirements
@@ -38,17 +27,6 @@ sudo apt install libcurl4-openssl-dev
--- ---
## Authentication
Generate your API key at:
Neocities > Settings > Manage Site Settings > API
(or https://neocities.org/settings/YOUR-USERNAME#api_key)
If you're coding a CLI or application, you must provide the key at runtime (env var, config file, etc).
---
## API Overview ## API Overview
### Info (public) ### Info (public)
@@ -96,7 +74,11 @@ Deletes files from the site.
### API Key ### API Key
For security reasons, you should get the API key via Neocities settings. This function is not avaiable. ```c
int neocities_apikey(const char *user, const char *pass, char **out);
```
Get your API key.
--- ---
@@ -106,28 +88,6 @@ See example.c
--- ---
## Security Notes
* TLS verification is enforced
* Redirects are disabled intentionally
* API key is never persisted internally
Recommended:
* Store key in env vars
* Avoid hardcoding
---
## Design Philosophy
* No login/password support
* API key only
* Explicit memory ownership
* Minimal abstraction
---
## License ## License
[MIT](LICENSE) [MIT](LICENSE)

View File

@@ -3,11 +3,15 @@
#include <stdlib.h> #include <stdlib.h>
int main() { int main() {
static const char* username = "YOUR_USER_NAME"; static const char* username = "YOUR_USERNAME";
static const char* api_key = "YOUR_API_KEY"; static const char* api_key = "YOUR_API_KEY";
char* out = NULL; char* out = NULL;
// GET /api/key
if (neocities_apikey(username, "YOUR_PASSWORD", &out) == 0) printf("%s", out);
free(out);
// GET /api/info // GET /api/info
if (neocities_info(username, &out) == 0) printf("%s", out); if (neocities_info(username, &out) == 0) printf("%s", out);
free(out); free(out);

View File

@@ -239,29 +239,35 @@ int neocities_upload(
// ------------------------- // -------------------------
// key — GET /api/key // key — GET /api/key
// ------------------------- // -------------------------
int neocities_apikey(const char *user, const char *pass, char **out) {
struct response resp = {0};
int neocities_apikey( char userpass[256];
const char *user, snprintf(userpass, sizeof(userpass), "%s:%s", user, pass);
const char *pass,
char **out
) CURL *curl = curl_easy_init();
{ if (!curl) return 1;
CURL *curl;
CURLcode resp;
char buf[128]; resp.data = malloc(1);
snprintf(buf, sizeof(buf), "%s:%s", user, pass); resp.len = 0;
resp.data[0] = '\0';
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
CURLcode rc = curl_easy_perform(curl);
if(rc == CURLE_OK) {
*out = resp.data;
} else {
return 1;
}
return 0; curl_easy_setopt(curl, CURLOPT_URL, "https://neocities.org/api/key");
curl_easy_setopt(curl, CURLOPT_USERPWD, userpass); // aqui é equivalente ao -u do curl
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
CURLcode res = curl_easy_perform(curl);
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
curl_easy_cleanup(curl);
if (res != CURLE_OK || http_code >= 400) {
free(resp.data);
return 2;
}
*out = resp.data; // resposta da API
return 0;
} }

View File

@@ -10,5 +10,6 @@ int neocities_info(const char *sitename, char **out);
int neocities_list(const char *api_key, const char *path, char **out); int neocities_list(const char *api_key, const char *path, char **out);
int neocities_delete(const char *api_key, const char **filenames, size_t count, char **response); int neocities_delete(const char *api_key, const char **filenames, size_t count, char **response);
int neocities_upload(const char *api_key, const char **local_files, const char **remote_names, size_t count, char **response); int neocities_upload(const char *api_key, const char **local_files, const char **remote_names, size_t count, char **response);
int neocities_apikey(const char *user, const char *pass, char **out);
#endif // NEOCITIES_H #endif // NEOCITIES_H