> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt # Method name: createBadge(badge: Badge) # Method package: wixMembersV2 # Method menu location: wixMembersV2 --> badges --> createBadge # Method Link: https://dev.wix.com/docs/velo/apis/wix-members-v2/badges/create-badge.md # Method Description: Creates a badge. The `createBadge()` function returns a Promise that resolves to the newly created badge. New badges do not have any badge permissions by default. You can set [badge permissions](https://support.wix.com/en/article/site-members-creating-and-managing-member-badges) from the Badges page in the Dashboard. If `backgroundColor` or `textColor` are not specified, they default to `"#796EFF"` (purple) and `"#FFFFFF"` (white) respectively. # Method Code Examples: *** Note: do not assume any prop names or enum values other than the ones in the example. ## Create a new badge. (dashboard page code) ```javascript import { badges } from 'wix-members-backend'; /* Sample badge value: * { * backgroundColor: "#42A5F5", * description: "Recognized Contributor Badge", * icon: "http://example.com/badge-icon.svg", * PermissionsEnabled: true, * textColor: "#FFFFFF, * title: "Contributor Badge * } * } */ export async function myCreateBadgeFunction(badge) { try { const newBadge = await badges.createBadge(badge); console.log('New badge created: ', newBadge); return newBadge; } catch (error) { console.error(error); // Handle the error } } /* Promise resolves to: * { * "_createdDate": "2024-01-18T13:08:22.116Z", * "_id": "b025f7a8-9c3e-4f5d-a2e7-8dc1bf3ca0f1", * "_updatedDate": "2024-01-18T13:08:22.116Z", * "backgroundColor": "#796EFF", * "description": "Recognized Contributor", * "icon": "http://example.com/badge-icon.svg", * "permissionsEnabled": false, * "slug": "contributor-badge-1", * "textColor": "#FFFFFF", * "title": "Contributor Badge" * } */ ``` ## Create a new badge. (export from backend code) ```javascript import { Permissions, webMethod } from 'wix-web-module'; import { badges } from 'wix-members-backend'; import { elevate } from 'wix-auth'; /* Sample badge value: * { * backgroundColor: "#42A5F5", * description: "Recognized Contributor Badge", * icon: "http://example.com/badge-icon.svg", * PermissionsEnabled: true, * textColor: "#FFFFFF, * title: "Contributor Badge * } * } */ export const myCreateBadgeFunction = webMethod(Permissions.Anyone, async (badge) => { try { const elevatedCreateBadge = elevate(badges.createBadge); const newBadge = await elevatedCreateBadge(badge); console.log('New badge created: ', newBadge); return newBadge; } catch (error) { console.error(error); // Handle the error } }); /* Promise resolves to: * { * "_createdDate": "2024-01-18T13:08:22.116Z", * "_id": "b025f7a8-9c3e-4f5d-a2e7-8dc1bf3ca0f1", * "_updatedDate": "2024-01-18T13:08:22.116Z", * "backgroundColor": "#796EFF", * "description": "Recognized Contributor", * "icon": "http://example.com/badge-icon.svg", * "permissionsEnabled": false, * "slug": "contributor-badge-1", * "textColor": "#FFFFFF", * "title": "Contributor Badge" * } */ ``` ## Create an expert contributor badge. ```javascript import { Permissions, webMethod } from 'wix-web-module'; import { badges } from 'wix-members-backend'; import { elevate } from 'wix-auth'; import wixData from 'wix-data'; /* Sample badge value: * { * backgroundColor: "#42A5F5", * description: "Expert Contributor Badge", * icon: "http://example.com/expert-badge-icon.svg", * permissionsEnabled: true, * textColor: "#FFFFFF", * title: "Expert Contributor Badge" * } * } */ export const createExpertContributorBadge = webMethod(Permissions.Anyone, async (memberId) => { const EXPERT_THRESHOLD = 5; // Minimum number of blog posts to be considered an expert contributor const SUBJECT_KEYWORD = 'velo'; // Subject keyword for blog posts // Count the number of blog posts created by the member with the given ID on the subject of 'velo' const blogPostsCount = await wixData.query('BlogPosts') .eq('author', memberId) .contains('tags', SUBJECT_KEYWORD) .count(); // Count if the member meets the criteria to be an expert contributor if (blogPostsCount >= EXPERT_THRESHOLD) { const expertBadge = { backgroundColor: "#42A5F5", description: "Expert Contributor Badge", icon: "http://example.com/expert-badge-icon.svg", permissionsEnabled: true, textColor: "#FFFFFF", title: "Expert Contributor Badge" }; try { const elevatedCreateBadge = elevate(badges.createBadge); const newExpertBadge = await elevatedCreateBadge(expertBadge); console.log('Expert badge created for member ', memberId, ': ', newExpertBadge); return newExpertBadge; } catch (error) { console.error('Error creating expert badge:', error); // Handle the error } } else { console.log('Member with ID ', memberId, ' does not meet the criteria for the expert badge.'); } }); /* Promise resolves to: * { * "_createdDate": "2024-02-02T12:00:00.000Z", * "_id": "unique-id-for-expert-badge-1", * "_updatedDate": "2024-02-02T12:00:00.000Z", * "backgroundColor": "#42A5F5", * "description": "Expert Contributor Badge", * "icon": "http://example.com/expert-badge-icon.svg", * "permissionsEnabled": true, * "slug": "expert-contributor-badge-1", * "textColor": "#FFFFFF", * "title": "Expert Contributor Badge" * } */ ``` ---