-
Notifications
You must be signed in to change notification settings - Fork 11
Project API Doc
Ankit K. edited this page Mar 4, 2025
·
1 revision
This document provides a comprehensive overview of the Core Stack Backend API endpoints, their functionality, and usage.
The API uses JWT (JSON Web Tokens) for authentication. Here's how the authentication flow works:
-
Registration/Login:
- When a user registers or logs in, they receive both an access token and a refresh token.
-
Using Access Tokens:
- The access token must be included in the Authorization header for all authenticated API requests:
Authorization: Bearer <your_access_token>- Access tokens are valid for 2 days.
-
Token Refresh:
- When an access token expires, use the refresh token to obtain a new one.
- Send a POST request to the token refresh endpoint with the refresh token.
- You'll receive a new access token (and potentially a new refresh token).
- Refresh tokens are valid for 14 days.
-
Logout:
- When logging out, send the refresh token to the logout endpoint to invalidate it.
- This prevents the refresh token from being used to obtain new access tokens.
-
URL:
/api/v1/auth/register/ - Method: POST
- Description: Register a new user in the system
-
Request Body:
{ "username": "user123", "email": "user@example.com", "password": "securepassword", "password_confirm": "securepassword", "first_name": "John", "last_name": "Doe", "contact_number": "1234567890", "organization": "optional-organization-uuid" } - Response: Returns the created user object along with access and refresh tokens
-
Notes:
- Organization ID is optional
- If provided, the user will be associated with that organization
-
URL:
/api/v1/auth/register/available_organizations/ - Method: GET
- Description: Get a list of organizations that users can select during registration
- Authentication: Not required
- Response: List of active organizations with their IDs and names
-
URL:
/api/v1/auth/login/ - Method: POST
- Description: Authenticate a user and obtain JWT tokens
-
Request Body:
{ "username": "user123", "password": "securepassword" } - Response: Returns access token, refresh token, and user details
- Notes: The access token must be included in the Authorization header for subsequent API calls
-
URL:
/api/v1/auth/token/refresh/ - Method: POST
- Description: Obtain a new access token using a valid refresh token
-
Request Body:
{ "refresh": "your-refresh-token" } - Response: Returns a new access token and, if configured, a new refresh token
-
Notes:
- Use this endpoint when your access token expires but your refresh token is still valid
- With the current configuration, refresh tokens are valid for 14 days while access tokens are valid for 2 days
- The system is configured with token rotation, meaning you'll receive a new refresh token with each refresh
- The old refresh token is automatically blacklisted after use
-
URL:
/api/v1/auth/logout/ - Method: POST
- Description: Logout a user by blacklisting their refresh token
-
Request Body:
{ "refresh_token": "your-refresh-token" } - Response: Success message
- Authentication: Required
- Notes: This invalidates the refresh token, preventing it from being used to obtain new access tokens
-
URL:
/api/v1/users/ - Method: GET
- Description: List users based on permissions
- Authentication: Required
-
Notes:
- Super admins see all users
- Organization admins see users in their organization
- Regular users see only themselves
-
URL:
/api/v1/users/ - Method: POST
- Description: Create a new user (admin function)
-
Request Body:
{ "username": "newuser", "email": "newuser@example.com", "password": "securepassword", "organization": "organization-id", "is_superadmin": false } - Authentication: Required
- Permissions: Super admin or organization admin
- Notes: Organization admins can only create users in their own organization
-
URL:
/api/v1/users/{user_id}/ - Method: GET
- Description: Get details of a specific user
- Authentication: Required
-
Permissions:
- Users can view their own details
- Organization admins can view users in their organization
- Super admins can view any user
-
URL:
/api/v1/users/{user_id}/ - Method: PUT/PATCH
- Description: Update user details
- Authentication: Required
-
Permissions:
- Users can update their own details
- Organization admins can update users in their organization
- Super admins can update any user
- Notes: Certain fields (is_superadmin, is_staff) can only be modified by super admins
-
URL:
/api/v1/users/{user_id}/set_organization/ - Method: PUT
- Description: Assign a user to an organization
-
Request Body:
{ "organization_id": "organization-uuid" } - Authentication: Required
- Permissions: Super admin or organization admin of the target organization
-
URL:
/api/v1/users/{user_id}/set_group/ - Method: PUT
- Description: Assign a user to a group/role (e.g., Organization Admin)
-
Request Body:
{ "group_id": "group-id" } - Authentication: Required
- Permissions: Super admin only
-
Notes:
- For Organization Admin group, the user must already be assigned to an organization
- Special validation is performed for Organization Admin assignments
-
URL:
/api/v1/users/{user_id}/remove_group/ - Method: PUT
- Description: Remove a user from a group/role
-
Request Body:
{ "group_id": "group-id" } - Authentication: Required
- Permissions: Super admin only
-
URL:
/api/v1/organizations/ - Method: GET
- Description: List organizations based on permissions
- Authentication: Required
-
Notes:
- Super admins see all organizations
- Other users see only their organization
-
URL:
/api/v1/organizations/ - Method: POST
- Description: Create a new organization
-
Request Body:
{ "name": "Organization Name", "description": "Organization Description" } - Authentication: Required
- Permissions: Super admin only
-
URL:
/api/v1/organizations/{organization_id}/ - Method: GET
- Description: Get details of a specific organization
- Authentication: Required
-
Permissions:
- Super admins can view any organization
- Users can view their own organization
-
URL:
/api/v1/organizations/{organization_id}/ - Method: PUT/PATCH
- Description: Update organization details
- Authentication: Required
-
Permissions:
- Super admins can update any organization
- Organization admins can update their own organization
-
URL:
/api/v1/projects/ - Method: GET
- Description: List projects based on permissions
- Authentication: Required
-
Notes:
- Super admins see all projects
- Organization admins see all projects in their organization
- Other users see projects they have access to
-
URL:
/api/v1/projects/ - Method: POST
- Description: Create a new project
-
Request Body:
{ "name": "Project Name", "description": "Project Description", "organization": "organization-id", "created_by": "user-id", "updated_by": "user-id" } - Authentication: Required
- Permissions: Super admin or organization admin
-
URL:
/api/v1/projects/{project_id}/ - Method: GET
- Description: Get details of a specific project
- Authentication: Required
- Permissions: User must have access to the project
-
URL:
/api/v1/projects/{project_id}/ - Method: PUT/PATCH
- Description: Update project details
- Authentication: Required
- Permissions: Super admin, organization admin, or project manager
-
URL:
/api/v1/projects/{project_id}/ - Method: DELETE
- Description: Delete a project
- Authentication: Required
- Permissions: Super admin or organization admin
-
URL:
/api/v1/projects/{project_id}/apps/ - Method: GET
- Description: List apps enabled for a project
- Authentication: Required
- Permissions: User must have access to the project
-
URL:
/api/v1/projects/{project_id}/enable_app/ - Method: POST
- Description: Enable an app for a project
-
Request Body:
{ "app_type": "plantation" } - Authentication: Required
- Permissions: Super admin, organization admin, or project manager
- Notes: Valid app types include 'plantation' and 'watershed'
-
URL:
/api/v1/projects/{project_id}/apps/{app_id}/ - Method: PUT/PATCH
- Description: Update app settings
-
Request Body:
{ "enabled": true } - Authentication: Required
- Permissions: Super admin, organization admin, or project manager
-
URL:
/api/v1/projects/{project_id}/apps/{app_id}/ - Method: DELETE
- Description: Disable an app for a project
- Authentication: Required
- Permissions: Super admin, organization admin, or project manager
-
URL:
/api/v1/projects/{project_id}/users/ - Method: GET
- Description: List users assigned to a project with their roles
- Authentication: Required
- Permissions: User must have access to the project
-
URL:
/api/v1/projects/{project_id}/users/ - Method: POST
- Description: Assign a user to a project with a specific role
-
Request Body:
{ "user": "user-id", "group": "group-id" } - Authentication: Required
- Permissions: Super admin, organization admin, or project manager
- Notes: The group ID represents the role (e.g., Project Manager, Data Entry, Viewer)
-
URL:
/api/v1/projects/{project_id}/users/{user_project_id}/ - Method: PUT/PATCH
- Description: Update a user's role in a project
-
Request Body:
{ "group": "new-group-id" } - Authentication: Required
- Permissions: Super admin, organization admin, or project manager
-
URL:
/api/v1/projects/{project_id}/users/{user_project_id}/ - Method: DELETE
- Description: Remove a user from a project
- Authentication: Required
- Permissions: Super admin, organization admin, or project manager
-
URL:
/api/v1/projects/{project_id}/plantation/kml/ - Method: GET
- Description: List KML files uploaded for a plantation project
- Authentication: Required
- Permissions: User must have access to the project
-
URL:
/api/v1/projects/{project_id}/plantation/kml/ - Method: POST
- Description: Upload a new KML file for a plantation project
- Request Body: Multipart form data with 'file' and optional 'name'
- Authentication: Required
- Permissions: User must have upload permission for the project
-
Notes:
- Only .kml files are accepted
- Duplicate files (same hash) are rejected
- The file is automatically converted to GeoJSON
- The project's merged GeoJSON file is updated
-
URL:
/api/v1/projects/{project_id}/plantation/kml/{kml_id}/ - Method: GET
- Description: Get details of a specific KML file
- Authentication: Required
- Permissions: User must have access to the project
-
URL:
/api/v1/projects/{project_id}/plantation/kml/{kml_id}/ - Method: DELETE
- Description: Delete a KML file
- Authentication: Required
- Permissions: User must have delete permission for the project
- Notes: The project's merged GeoJSON file is updated after deletion
-
URL:
/api/v1/projects/{project_id}/watershed/plans/ - Method: GET
- Description: List watershed plans for a project
- Authentication: Required
- Permissions: User must have access to the project
-
URL:
/api/v1/projects/{project_id}/watershed/plans/ - Method: POST
- Description: Create a new watershed plan
-
Request Body:
{ "plan": "Plan Name", "state": "state-id", "district": "district-id", "block": "block-id", "village_name": "Village Name", "gram_panchayat": "Gram Panchayat Name" } - Authentication: Required
- Permissions: User must have create permission for the project
-
URL:
/api/v1/projects/{project_id}/watershed/plans/{plan_id}/ - Method: GET
- Description: Get details of a specific watershed plan
- Authentication: Required
- Permissions: User must have access to the project
-
URL:
/api/v1/projects/{project_id}/watershed/plans/{plan_id}/ - Method: PUT/PATCH
- Description: Update a watershed plan
- Authentication: Required
- Permissions: User must have update permission for the project
-
URL:
/api/v1/projects/{project_id}/watershed/plans/{plan_id}/ - Method: DELETE
- Description: Delete a watershed plan
- Authentication: Required
- Permissions: User must have delete permission for the project
These endpoints are maintained for backward compatibility:
-
URL:
/api/v1/get_plans/ - Method: GET
- Description: Get all plans
-
URL:
/api/v1/add_plan/ - Method: POST
- Description: Add a new plan
-
URL:
/api/v1/add_resources/ - Method: POST
- Description: Add resources to a plan
-
URL:
/api/v1/add_works/ - Method: POST
- Description: Add works to a plan
-
URL:
/api/v1/sync_offline_data/{resource_type}/ - Method: POST
- Description: Synchronize offline data for a specific resource type
-
Register a new user:
curl -X POST http://api.example.com/api/v1/auth/register/ \ -H "Content-Type: application/json" \ -d '{"username": "newuser", "email": "newuser@example.com", "password": "securepassword"}'
-
Login with the new user:
curl -X POST http://api.example.com/api/v1/auth/login/ \ -H "Content-Type: application/json" \ -d '{"username": "newuser", "password": "securepassword"}'
-
Refresh your access token when it expires:
curl -X POST http://api.example.com/api/v1/auth/token/refresh/ \ -H "Content-Type: application/json" \ -d '{"refresh": "your-refresh-token"}'
-
Use the access token for authenticated requests:
curl -X GET http://api.example.com/api/v1/projects/ \ -H "Authorization: Bearer {access_token}"
-
Create a project (if not already created):
curl -X POST http://api.example.com/api/v1/projects/ \ -H "Authorization: Bearer {access_token}" \ -H "Content-Type: application/json" \ -d '{"name": "Plantation Project", "description": "A new plantation project", "organization": "organization-id"}'
-
Enable the plantation app for the project:
curl -X POST http://api.example.com/api/v1/projects/{project_id}/apps/ \ -H "Authorization: Bearer {access_token}" \ -H "Content-Type: application/json" \ -d '{"app_type": "plantation", "enabled": true}' -
Upload a KML file:
curl -X POST http://api.example.com/api/v1/projects/{project_id}/plantation/kml/ \ -H "Authorization: Bearer {access_token}" \ -F "file=@/path/to/plantation.kml" \ -F "name=Plantation Boundaries" -
View the uploaded KML files:
curl -X GET http://api.example.com/api/v1/projects/{project_id}/plantation/kml/ \ -H "Authorization: Bearer {access_token}"
-
Create a project (if not already created):
curl -X POST http://api.example.com/api/v1/projects/ \ -H "Authorization: Bearer {access_token}" \ -H "Content-Type: application/json" \ -d '{"name": "Watershed Project", "description": "A new watershed project", "organization": "organization-id"}'
-
Enable the watershed app for the project:
curl -X POST http://api.example.com/api/v1/projects/{project_id}/apps/ \ -H "Authorization: Bearer {access_token}" \ -H "Content-Type: application/json" \ -d '{"app_type": "watershed", "enabled": true}' -
Create a watershed plan:
curl -X POST http://api.example.com/api/v1/projects/{project_id}/watershed/plans/ \ -H "Authorization: Bearer {access_token}" \ -H "Content-Type: application/json" \ -d '{ "plan": "Watershed Plan 2023", "state": "state-id", "district": "district-id", "block": "block-id", "village_name": "Example Village", "gram_panchayat": "Example GP" }' -
View the created watershed plans:
curl -X GET http://api.example.com/api/v1/projects/{project_id}/watershed/plans/ \ -H "Authorization: Bearer {access_token}"
- Authentication: All API endpoints (except registration and login) require JWT authentication.
-
Authorization: Permissions are checked at multiple levels:
- Organization level
- Project level
- Feature-specific permissions
- Token Expiration: Access tokens expire after 2 days
- Token Refresh: Refresh tokens (valid for 14 days) can be used to obtain new access tokens
- Token Blacklisting: Refresh tokens are blacklisted on logout or when used for refresh
- Token Rotation: When refreshing a token, a new refresh token is issued and the old one is blacklisted