workos-create-org
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v13View latest version
This Val provides an HTTP endpoint for creating organizations and organization memberships in WorkOS with JWT authentication.
- JWT Authentication: Validates WorkOS JWTs using JWKS
- Organization Creation: Creates new organizations in WorkOS
- Membership Management: Automatically creates admin membership for the authenticated user
- Error Handling: Comprehensive error handling for WorkOS API and JWT validation
Creates a new organization and organization membership for the authenticated user.
Authentication: Bearer token (WorkOS JWT)
Request Body:
{ "name": "Organization Name", "domains": ["example.com"] // optional }
Response:
{ "organization": { "id": "org_123", "name": "Organization Name", "domains": ["example.com"], "createdAt": "2023-01-01T00:00:00.000Z", "updatedAt": "2023-01-01T00:00:00.000Z" }, "membership": { "id": "om_456", "userId": "user_789", "organizationId": "org_123", "role": "admin", "status": "active" } }
Health check endpoint.
Response:
{ "status": "ok", "timestamp": "2023-01-01T00:00:00.000Z" }
The following environment variables must be configured in Val Town:
WORKOS_API_KEY: Your WorkOS API key (starts withsk_)JWKS_URL: The JWKS URL for JWT verification (e.g.,https://api.workos.com/sso/jwks/{connection_id})
// Client-side usage
const response = await fetch('https://your-val-url.web.val.run/create-organization', {
method: 'POST',
headers: {
'Authorization': `Bearer ${workosJWT}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'My New Organization',
domains: ['mycompany.com']
})
});
const result = await response.json();
console.log('Created organization:', result.organization);
The API returns appropriate HTTP status codes and error messages:
400: Bad request (missing organization name, WorkOS API errors)401: Unauthorized (missing/invalid JWT token)500: Internal server error (configuration issues, unexpected errors)
- JWT tokens are verified using JWKS from WorkOS
- Only authenticated users can create organizations
- Users are automatically assigned admin role in organizations they create
- All WorkOS API calls are made server-side to protect API keys
curl https://your-val-url.web.val.run/health
curl https://your-val-url.web.val.run/
curl -X POST https://your-val-url.web.val.run/create-organization \ -H "Content-Type: application/json" \ -d '{"name": "Test Organization"}'
curl -X POST https://your-val-url.web.val.run/create-organization \ -H "Authorization: Bearer invalid-token" \ -H "Content-Type: application/json" \ -d '{"name": "Test Organization"}'
curl -X POST https://your-val-url.web.val.run/create-organization \ -H "Authorization: Bearer YOUR_WORKOS_JWT_TOKEN" \ -H "Content-Type: application/json" \ -d '{"name": "My New Organization", "domains": ["example.com"]}'